NodeJS操作MongoDB的Dao层封装

写在前面:
最近读了一本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;

使用方法同模块一

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来了-小老弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值