自己用Nodejs+es6语法+单例模式 封装的Express、 Koa、 原生nodejs操作MongoDb数据库的类库,用es6封装的Nodejs操作Mongodb的类库。
用法非常简单演示一下: 查找数据:
var result=await DB.find(‘user’,{});
增加数据
let data=await DB.insert(‘user’,ctx.request.body);
** 更多用法…**
你也可以通过下面百度网盘直接获取视频+源码 https://pan.baidu.com/s/1KNaA97kGwNhavch5rP_G7w
百度网盘地址失效访问:
文件1:一共两个文件 第一个config.js --------------------------开始--------------------------- var app={ dbUrl: ‘mongodb://localhost:27017/’, dbName: ‘koa’ } module.exports=app; --------------------------结束---------------------------
文件2:一共两个文件 第二个db.js --------------------------开始---------------------------
var MongoDB=require(‘mongodb’); var MongoClient =MongoDB.MongoClient; const ObjectID = MongoDB.ObjectID; var Config=require(’./config.js’); class Db{ static getInstance(){ /1、单例 多次实例化实例不共享的问题/
    if(!Db.instance){
        Db.instance=new Db();
    }
    return  Db.instance;
}
constructor(){
    this.dbClient=''; /*属性 放db对象*/
    this.connect();   /*实例化的时候就连接数据库*/
}
connect(){  /*连接数据库*/
  let _that=this;
  return new Promise((resolve,reject)=>{
      if(!_that.dbClient){         /*1、解决数据库多次连接的问题*/
          MongoClient.connect(Config.dbUrl,(err,client)=>{
              if(err){
                  reject(err)
              }else{
                  _that.dbClient=client.db(Config.dbName);
                  resolve(_that.dbClient)
              }
          })
      }else{
          resolve(_that.dbClient);
      }
  })
}
find(collectionName,json){
   return new Promise((resolve,reject)=>{
        this.connect().then((db)=>{
            var result=db.collection(collectionName).find(json);
            result.toArray(function(err,docs){
                if(err){
                    reject(err);
                    return;
                }
                resolve(docs);
            })
        })
    })
}
update(collectionName,json1,json2){
    return new Promise((resolve,reject)=>{
            this.connect().then((db)=>{
                //db.user.update({},{$set:{}})
                db.collection(collectionName).updateOne(json1,{
                    $set:json2
                },(err,result)=>{
                    if(err){
                        reject(err);
                    }else{
                        resolve(result);
                    }
                })
            })
    })
}
insert(collectionName,json){
    return new  Promise((resolve,reject)=>{
        this.connect().then((db)=>{
            db.collection(collectionName).insertOne(json,function(err,result){
                if(err){
                    reject(err);
                }else{
                    resolve(result);
                }
            })
        })
    })
}
remove(collectionName,json){
    return new  Promise((resolve,reject)=>{
        this.connect().then((db)=>{
            db.collection(collectionName).removeOne(json,function(err,result){
                if(err){
                    reject(err);
                }else{
                    resolve(result);
                }
            })
        })
    })
}
getObjectId(id){    /*mongodb里面查询 _id 把字符串转换成对象*/
    return new ObjectID(id);
}
}
module.exports=Db.getInstance();
--------------------------结束---------------------------
更多用法… ** 你也可以通过下面百度网盘直接获取视频+源码:** https://pan.baidu.com/s/1KNaA97kGwNhavch5rP_G7w
百度网盘地址失效访问: