NodeJS操作MongoDB的Dao層封裝
阿新 • • 發佈:2018-12-24
寫在前面:
最近讀了一本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;
使用方法同模組一