ES6封装MongoDB的CRUD

    科技2022-07-11  97

    node.js调用mangodb的操作还是有点繁琐的,为了简化底层操作,关注具体业务,我特意把MongoDB封装了一下,话不多说直接看代码

    封装代码

    let MongoClient = require('mongodb').MongoClient; const url = "mongodb://127.0.0.1:27017/" class Dbc { /* @dataName 操作的数据库名 @colName 指定操作的集合名 */ constructor(dataName,colName) { this.dataName = dataName this.colName = colName } //查询,使用Promise封装,查询后的数据操作在.then()中完成 find(obj) { let that=this return new Promise((resolve,reject) => { MongoClient.connect(url,{useNewUrlParser:true},function (err,dbs) { if (err) { reject(err) } else { let collection=dbs.db(that.dataName).collection(that.colName) collection.find(obj).toArray(function (err,data) { if (err) reject(err) else resolve(data) dbs.close() }) } }) }) } //插入一个对象 insertOne(obj){ let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.insertOne(obj,function(err, res) { if (err) throw err; console.log("文档插入成功:",res.result) dbs.close(); }) }) } //插入一个对象数组 insertMany(obj){ let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.insertMany(obj,function(err, res) { if (err) throw err; console.log("插入的文档数量为: " + res.insertedCount) dbs.close(); }) }) } //插入一个数组对象 insertMany(obj){ let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.insertMany(obj,function(err, res) { if (err) throw err; console.log("插入的文档数量为: " + res.insertedCount) dbs.close(); }) }) } //删除一个,删除第一个满足对象 deleteOne(setObj) { let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.deleteOne(setObj,function(err) { if (err) throw err; console.log("文档删除成功" ) dbs.close(); }) }) } //删除所有满足条件的对象 deleteMany(setObj) { let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.deleteMany(setObj,function(err,obj) { if (err) throw err; console.log(obj.result.n + " 条文档被删除") dbs.close(); }) }) } //更新一条数据,第一个满足条件的对象 updateOne(whereObj,setObj) { let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.updateOne(whereObj,{$set:setObj},function(err) { if (err) throw err; console.log("文档更新成功") dbs.close(); }) }) } //更新多条数据 updateMany(whereObj,setObj) { let that = this; MongoClient.connect(url,function(err,dbs){ if (err) throw err; let collection=dbs.db(that.dataName).collection(that.colName) collection.updateMany(whereObj,{$set:setObj},function(err,res) { if (err) throw err; console.log(res.result.nModified + " 条文档被更新") dbs.close(); }) }) } } module.exports={ Dbc }

    调用代码

    const {Dbc} = require("./mongodbc") const dbc = new Dbc("mydb","user") //添加 var myobj = [ { name: '菜鸟工具', url: 'https://c.runoob.com', type: 'cn'}, { name: 'Google', url: 'https://www.google.com', type: 'en'}, { name: 'Facebook', url: 'https://www.google.com', type: 'en'} ]; dbc.insertMany(myobj) //删除type为en的数据 dbc.deleteMany({type:"en"}) //修改 dbc.updateMany({type:"en"},{url:"这是被修改后的路径"}) //查询type为en的数据 dbc.find({type:"en"}).then(data => { console.log(data) })
    Processed: 0.031, SQL: 8