ES6封裝MongoDB的CRUD

沒人看的垃圾部落格發表於2020-10-03

node.js呼叫mangodb的操作還是有點繁瑣的,為了簡化底層操作,關注具體業務,我特意吧MongoDB封裝了一下,話不多說直接看程式碼

封裝程式碼

let MongoClient = require('mongodb').MongoClient;
const url = "mongodb://127.0.0.1:27017/"

class Dbc {
  constructor(dataName,colName)
  {
    this.dataName = dataName
    this.colName = colName
  }

  //查詢
  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)
  })

相關文章