node.js使用ES6新增面向对象对mongodb数据库的增删改查操作

1、要使用mongodb数据库首先需要下载安装mongodb
官网下载:https://docs.mongodb.com/
2、上模块代码(modlue.js)

let mongodb =require('mongodb');//引进数据库模块
let mongoClient = mongodb.MongoClient;
const url = 'mongodb://127.0.0.1:27017';
class Db{
    /*
        @database 数据库名字
        @colName  集合名字
     */
    constructor(database,colName){
        this.database = database;
        this.colName = colName;
    }
    /*
        查询数据
        @obj  查询的对象
        @cb   回调函数
     */
    find(obj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){ //判断是否出错,err为null则成功
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.find(obj).toArray(function(err,data){
                        dbs.close(); //关闭数据库
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb(data);
                        }
                    });
                })
            }
            
        })
    }
    /*
        插入一条数据
        @obj  查询的对象
        @cb   回调函数
     */
    insertOne(obj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.insertOne(obj,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })
    }
    /*
        插入多条数据
        @obj  查询的对象
        @cb   回调函数
     */
    insertMany(obj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.insertMany(obj,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })
    }
    /*
        删除一条数据
        @obj  查询的对象
        @cb   回调函数
     */
    deleteOne(obj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.deleteOne(obj,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })
    }
    /*
        删除多条数据
        @obj  查询的对象
        @cb   回调函数
     */
    deleteMany(obj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.deleteMany(obj,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })
    }
    /*
        更新一条数据
        @obj  查询的对象
        @obj1  更改内容
        @cb   回调函数
     */
    update(obj,newobj,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.update(obj,newobj,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })

    }
    /*
        更新多条数据
        @obj  查询的对象
        @obj1  更改内容
        @cb   回调函数
     */
    updateMany(obj,obj1,cb){
        let that = this;
        mongoClient.connect(url,function(err,dbs){
            if(err){
                console.log(err);
            }else{
                let db = dbs.db(that.database);
                db.createCollection(that.colName).then(function(collection){
                    collection.updateMany(obj,obj1,function(err){
                        dbs.close();
                        if(err){
                            console.log(err);
                        }else{
                            typeof cb =='function' && cb();
                        }
                    });
                })
            }  
        })

    }
}
module.exports = Db; //暴露

3、使用模块代码(use.js)

let Db = require('./modlue.js'); //自定义引进模块
let mongodb = new Db('test','user');//实例对象,并传参
mongodb.insertOne({name:'丫1',age:'20'},function(){
    mongodb.find({},function(data){
        console.log(data);
    });
})

然后可以开启MongoDB服务后,执行:node use.js

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值