nodeJS 操作 mongodb 增删改查 |
config.js module.exports={ dbName_test:"test",//测试数据库表 logUrl:'logs/dbErr.log',//错误日志 } |
timeFormat.js let date=new Date(); let Y=date.getFullYear(); let m=date.getMonth()+1; let d=date.getDate(); let h=date.getHours(); let M=date.getMinutes(); let s=date.getSeconds(); module.exports=Y+"-"+(m>9 ? m : '0'+m)+"-"+(d>9 ? d :'0'+d)+" "+(h>9? h:'0'+h)+":"+(M>9? M:'0'+M)+":"+(s>9?s:'0'+s)+'\n'; |
db.js const mongodb=require('mongodb'); const logs=require('./log'); const timeFormat=require('./timeFormat'); const MongoClient = require('mongodb').MongoClient; const config=require('./../config/index'); // Connection URL const url = 'mongodb://root:root123@localhost:27017'; const logUrl=config.logUrl; //连接数据库 const db=function (dbName) { new Promise(function (resolve,reject) { // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { if(err){ logs(logUrl,timeFormat+'数据库连接失败\n'); return false } const db = client.db(dbName); resolve(db); }); }); }; //插入一条数据 exports.insertOne=function(dbName,collection,json) { return new Promise(function (resolve,reject) { //插入之前先查询 find(collection,json).then(function (data) { if(data.length===0){ db(dbName).then(function (db) { db.collection(collection).insertOne(json,function (err,res) { if(err){ logs(logUrl,timeFormat+'数据插入失败'+err+'\n'); return false } resolve(res); }); }) }else { resolve('已存在,请勿重复添加') } }); }) }; //插入多条数据 exports.insertMany=function (dbName,collection,Arr) { return new Promise(function (resolve, reject) { db(dbName).then(function (db) { db.collection(collection).insertMany(Arr,function (err,res) { if(err){ logs(logUrl,timeFormat+'数据插入失败'+err+'\n'); return false } resolve(res); }) }) }) }; //查询数据 function find(dbName,collection,whereStr={}) { return new Promise(function (resolve, reject) { db(dbName).then(function (db) { db.collection(collection).find(whereStr).toArray(function (err,res) { if(err){ logs(logUrl,timeFormat+'数据查询失败'+err+'\n'); return false } resolve(res); }); }) }) } exports.find=find; //更新一条数据 exports.updateOne=function (dbName,collection,whereStr, updateStr) { return new Promise(function (resolve,reject) { db(dbName).then(function (db) { db.collection(collection).updateOne(whereStr,{$set: updateStr},function (err,res) { if(err){ logs(logUrl,timeFormat+'数据更新失败'+err+'\n'); resolve('数据更新失败'); return false } resolve(res); }) }) }) }; //更新多条数据 exports.updateMany=function (dbName,collection,whereStr,updateStr) { return new Promise(function (resolve,reject) { db(dbName).then(function (db) { db.collection(collection).updateMany(whereStr,{$set:updateStr},function (err,res) { if(err){ logs(logUrl,timeFormat+'批量更新数据失败'+err+'\n'); resolve('批量更新数据失败'); return false } resolve(res) }); }) }) }; //删除一条数据 exports.deleteOne=function (dbName,collection,whereStr) { return new Promise(function (resolve, reject) { db(dbName).then(function (db) { db.collection(collection).deleteOne(whereStr,function (err,res) { if(err){ logs(logUrl,timeFormat+'数据删除失败'+err+'\n'); resolve('数据删除失败'); return false } resolve(res); }) }) }) } //删除多条数据 exports.deleteMany=function (dbName,collection,whereStr) { return new Promise(function (resolve, reject) { db(dbName).then(function (db) { db.collection(collection).deleteMany(whereStr,function (err, res) { if(err){ logs(logUrl,timeFormat+'批量删除数据失败'+err+'\n'); resolve('批量删除数据失败'); return false } }) }); }) } |
app.js 调用 const db=require('./untils/db'); const dbName = config.dbName_test; //插入一条数据 db.insertOne('haha',{"name":"this is haha"}).then(function (data) { console.log(data) }) //查找数据 db.find(dbName,'haha').then(function (data) { console.log(data) }); //插入多条数据 db.insertMany(dbName,'haha',[ {"name":"this is haha"}, {"name1":"this is haha 1"}, {"name2":"this is haha 2"}, ]).then(function (data) { console.log(data) }) //更新多条数据 db.updateMany(dbName,'haha',{"name":"改成中文的了"},{'name':"change english"}).then(function (data) { console.log(data) }) //删除多条数据 db.deleteMany(dbName,'haha',{name:"change english"}).then(function (data) { console.log(data) }) |