1. 程式人生 > >Node.js封裝對mongodb操作的模組

Node.js封裝對mongodb操作的模組

var mongodb=require("mongodb");
var MongoClient=mongodb.MongoClient;
var connStr="mongodb://127.0.0.1:27017/";
//連線資料庫
function _connect(cb){
    MongoClient.connect(connStr,function(err,client){
        if(err){
            console.log("失敗");
        }else{
            //指定資料庫的名字"bang"
            var db=client.db("bang"
); cb(db); } }) } /***********************插入*******************************/ //插入一條記錄 module.exports.insertOne=function(collection,obj,cb){ _connect(function(db){ db.collection(collection).insertOne(obj,function(err,results){ cb(err,results); }) }) } //插入多條記錄
/* * collection:插入的集合, * arr:插入的文件 * cb:回撥函式。通過該函式返回執行的結果*/ module.exports.insertMany=function(collection,arr,cb){ _connect(function(db){ db.collection(collection).insertMany(arr,function(err,results){ cb(err,results); }) }) } /*********************查詢**********************************/
//根據條件查詢記錄數 module.exports.count=function(collection,whereObj,cb){ _connect(function(db){ db.collection(collection).count(whereObj).then(function(count){ cb(count); }) }) } /*查詢 * collection:集合 * obj: * whereObj:條件,預設是{} * sortObj:排序,預設是{} * limit:顯示提定條數,預設是0 * skip:跳過指定條數,預設是0*/ module.exports.find=function(collection,obj,cb){ //如果有條件,將條件賦值給obj.whereObj,沒有傳條件預設為{} obj.whereObj=obj.whereObj||{}; obj.sortObj=obj.sortObj||{}; obj.limit=obj.limit||0; obj.skip=obj.skip||0; _connect(function(db){ db.collection(collection) .find(obj.whereObj) .sort(obj.sortObj) .limit(obj.limit) .skip(obj.skip) .toArray(function(err,results){ cb(err,results); }) }) } /* * 查詢一條記錄*/ module.exports.findOne=function(collection,whereObj,cb){ _connect(function(db) { db.collection(collection).findOne(obj, function (err, results) { cb(err, results); }); }); } //根據ID來查詢記錄 module.exports.findOneById=function(collection,id,cb){ _connect(function(db) { db.collection(collection).findOne({_id: mongodb.ObjectId(id)}, function (err, results) { cb(err, results); }); }); } /*********************修改******************************************/ //根據ID修改一條記錄 module.exports.updateOneById=function(collection,id,upObj,cb){ _connect(function(db) { db.collection(collection).updateOne({_id:mongodb.ObjectId(id)}, upObj, function (err, results) { cb(err, results); }) }); } //修改一條記錄 module.exports.updateOne=function(collection,whereObj,upObj,cb){ _connect(function(db) { db.collection(collection).updateOne(whereObj, upObj, function (err, results) { cb(err, results); }) }); } //修改多條記錄 module.exports.updateMany=function(collection,whereObj,upObj,cb){ db.collection(collection).updateMany(whereObj,upObj,function(err,results){ cb(err,results); }) } /**********************刪除**************************************/ //根據ID來刪除一條記錄 module.exports.deleteOneById=function(collection,id,cb){ _connect(function(db) { db.collection(collection).deleteOne({_id: mongodb.ObjectId(id)}, function (err, results) { cb(err, results); }) }); } //刪除一條記錄 module.exports.deleteOne=function(collection,whereObj,cb){ _connect(function(db) { db.collection(collection).deleteOne(whereObj, function (err, results) { cb(err, results); }) }); } //刪除多條記錄 module.exports.deleteMany=function(collection,whereObj,cb){ _connect(function(db) { db.collection(collection).deleteMany(whereObj, function (err, results) { cb(err, results); }) }); }