mongodb的批量修改为什么要和修改器一起用
1个回答
展开全部
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var MD5DB = {
//数据库配置
_dbConf: {
dbName: 'comment', //数据库名称
serverIp: 'xxxxxx', //数据库访问ip,fedev机器ip
serverPort: xxxx //mongodb访问端口
},
init: function(){
},
itemIndex:0,
updateFilesModified:function(objArray,callback){
var dbConf = this._dbConf;
var self = this;
var db = new Db(dbConf.dbName, new Server(dbConf.serverIp, dbConf.serverPort), {safe: true});
db.open(function(err, db) {
var list = db.collection('files');
self.updateInstance(list, objArray,db);
});
},
updateInstance:function(list,items,db){
var self = this;
list.update({
fileId: items[self.itemIndex]
}, {$inc : {modified: 1 }}, {
upsert:true,
w: 1
}, function(err, item) {
if(err) console.log(err.stack);
self.itemIndex++;
if( self.itemIndex >= items.length){
db.close();
self.itemIndex =0;
}else{
self.updateInstance(list,items,db)
}
});
}
};
module.exports = MD5DB;
var Server = require('mongodb').Server;
var MD5DB = {
//数据库配置
_dbConf: {
dbName: 'comment', //数据库名称
serverIp: 'xxxxxx', //数据库访问ip,fedev机器ip
serverPort: xxxx //mongodb访问端口
},
init: function(){
},
itemIndex:0,
updateFilesModified:function(objArray,callback){
var dbConf = this._dbConf;
var self = this;
var db = new Db(dbConf.dbName, new Server(dbConf.serverIp, dbConf.serverPort), {safe: true});
db.open(function(err, db) {
var list = db.collection('files');
self.updateInstance(list, objArray,db);
});
},
updateInstance:function(list,items,db){
var self = this;
list.update({
fileId: items[self.itemIndex]
}, {$inc : {modified: 1 }}, {
upsert:true,
w: 1
}, function(err, item) {
if(err) console.log(err.stack);
self.itemIndex++;
if( self.itemIndex >= items.length){
db.close();
self.itemIndex =0;
}else{
self.updateInstance(list,items,db)
}
});
}
};
module.exports = MD5DB;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询