1.1.1 • Published 6 years ago

mongo_pro v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

安装

    npm install mongo_pro

快速开始

    const app = require('express')();
    const DB = require('mongo_pro');
    app.use( DB{
        host: 'mongodb://localhost', //主机地址
        port: '27017',      //端口号
        database: 'test'    //链接数据库名称
    })

使用实例

1.find

    req.db.collection('user').find({'account':account},(err,result)=>{
        if(err) throw err;
        console.log(result)
    }
    

2.insert

    req.db.collection('user').insert({'account':account,'age':35},(err,result)=>{
        if(err) throw err;
        console.log(result)
    }
    

3.update

    req.db.collection('user').update({'account':account},{$set:{'age':34}},{upsert:true,multi:false},(err,result)=>{
        if(err) throw err;
        console.log(result)
    }
    

4.remove

    req.db.collection('user').remove({'account':account},(err,result)=>{
        if(err) throw err;
        console.log(result)
    }
    

5.skip,limit,sort

    req.da.collection('user').sort(_id:1).skip(2).limit(5).find({'account':account},(err,result)=>{
        if(err) throw err;
        console.log(result);
    })
    

6.aggregate

    req.db.collection('user').aggregate([{$group:{_id:'$account',result: {$sum:'$age'}}}],(err,result)=>{
        if(err) throw err;
        console.log(result)
    })