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)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/155198.html
標籤:其他
