NodeJS操作MongoDB的Dao层封装

匿名 (未验证) 提交于 2019-12-03 00:18:01

写在前面:
最近读了一本ES6的书,算是开启了我转型到js前端领域的一个契机,感概技术发展的如此迅速,于是又囫囵吞枣的学了reactjs,nodejs,mongodb基础知识。一直忙着学习,也没有腾出时间总结,这次在学习nodejs操作mongodb增删改查基础dao层操作时,自己照葫芦画瓢封装了两个模块,对于初学者也好理解,可以对比着看,使用起来也比较简单,提供给大家做做参考,不吝赐教。

模块一
命名为MongoDBHandler.js

function MongoDB(MongoClient,url,dbName,collectionName){     this.MongoClient = MongoClient;     this.url= url;     this.dbName = dbName;     this.collection = collectionName; }; //连接数据库 MongoDB.prototype.connect = function(url,callback){     if(!url){         return;     }     this.MongoClient.connect(url,function(err,db){         callback(err,db);         db.close();     }); } //插入单条数据 MongoDB.prototype.insertOne = function(oneObj,callback){     let dbName = this.dbName;     let collection = this.collection;     this.connect(this.url,function(err,db){         var client = db.db(dbName);         client.collection(collection).insertOne(oneObj, function(err, res) {             if (err) throw err;             console.log("单个文档插入成功");             if(callback){                 callback(err,res);             }         });     }); } //批量插入 MongoDB.prototype.insertMany = function(objs,callback){     if(!Array.isArray(objs)){         throw new Error("非数组,类型不匹配!");         return;     }      let dbName = this.dbName;     let collection = this.collection;     this.connect(this.url,function(err,db){         var client = db.db(dbName);         client.collection(collection).insertMany(objs, function(err, res) {             if (err) throw err;             console.log("批量文档插入成功");             if(callback){                 callback(err,res);             }         });     }); } //查找指定条件下的数据 MongoDB.prototype.find = function(whereStr,callback){     let dbName = this.dbName;     let collection = this.collection;     this.connect(this.url,function(err,db){         if (err) throw err;         var client = db.db(dbName);         client.collection(collection).find(whereStr).toArray(function(err, result) {             if (err) throw err;             console.log(result);             if(callback){                 callback(err,result);             }         });     }); } //删除 MongoDB.prototype.remove = function(whereStr,callback){     let dbName = this.dbName;     let collection = this.collection;     this.connect(this.url,function(err,db){         if (err) throw err;         var client = db.db(dbName);         client.collection(collection).remove(whereStr,function(err, result) {             if (err) throw err;             console.log(result);             if(callback){                 callback(err,result);             }         });     }); } //修改 MongoDB.prototype.update = function(data,updateData,callback){     let dbName = this.dbName;     let collection = this.collection;     this.connect(this.url,function(err,db){         if (err) throw err;         var client = db.db(dbName);         client.collection(collection).remove(data,updateData,function(err, result) {             if (err) throw err;             console.log(result);             if(callback){                 callback(err,result);             }         });     }); } module.exports = MongoDB;

模块一如何使用

var MongoClient1 = require('mongodb').MongoClient; var url1= "mongodb://localhost:27017/"; var dbName1 = "runoob"; var collection1 = "site2"; var oneObj = { name: "name1", url: "www.runoob" }; var myobj =  [             { name: 'name2', url: 'https://www.baidu.com', type: 'cn'},             { name: 'name3', url: 'https://www.google.com', type: 'en'},             { name: 'name4', url: 'https://www.google.com', type: 'en'}            ];  var mongodb = require("./MongoDBHandler");//引入包  var mongo = new mongodb(MongoClient1,url1,dbName1,collection1); mongo.insertOne(oneObj,function(err,res){//插入单条     console.log(res+ res.insertedCount); }); mongo.insertMany(myobj,function(err,res){     console.log(res,res.insertedCount); }); mongo.remove({"type":"cn"},function(err,res){     console.log(res); }) mongo.find({},function(err,res){     console.log(res); });

模块二
命名为MongoDBHandler.js

function MongoDB(MongoClient,url,dbName,collection){     //插入单条数据     this.insertOne = function(oneObj,callback){         MongoClient.connect(url,function(err,db){             var client = db.db(dbName);             client.collection(collection).insertOne(oneObj, function(err, res) {                 if (err) throw err;                 console.log("单个文档插入成功");                 if(callback){                     callback(err,res);                 }             });             db.close();         });     }     //批量插入     this.insertMany = function(objs,callback){         if(!Array.isArray(objs)){             throw new Error("非数组,类型不匹配!");             return;         }         MongoClient.connect(url,function(err,db){             var client = db.db(dbName);             client.collection(collection).insertMany(objs, function(err, res) {                 if (err) throw err;                 console.log("批量文档插入成功");                 if(callback){                     callback(err,res);                 }             });             db.close();         });     };     //查找指定条件下的数据     this.find = function(whereStr,callback){         MongoClient.connect(url,function(err,db){             if (err) throw err;             var client = db.db(dbName);             client.collection(collection).find(whereStr).toArray(function(err, result) {                 if (err) throw err;                 console.log(result);                 if(callback){                     callback(err,result);                 }             });             db.close();         });     }     //删除     this.remove = function(whereStr,callback){         MongoClient.connect(url,function(err,db){             if (err) throw err;             var client = db.db(dbName);             client.collection(collection).remove(whereStr,function(err, result) {                 if (err) throw err;                 console.log(result);                 if(callback){                     callback(err,result);                 }             });             db.close();         });     }     //修改     this.update = function(data,updateData,callback){         MongoClient.connect(url,function(err,db){             if (err) throw err;             var client = db.db(dbName);             client.collection(collection).remove(data,updateData,function(err, result) {                 if (err) throw err;                 console.log(result);                 if(callback){                     callback(err,result);                 }             });             db.close();         });     } }; module.exports = MongoDB;

使用方法同模块一

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!