mongodb 执行条数不确定问题

浏览:50日期:2023-06-14

问题描述

var all = db.getCollection(’logging’).find({'Properties.EnterpriseId':{$ne:null},'Properties.AccountId':null}).sort({'Date':-1});all.forEach( function(value,index,arr){ if(value.Properties.AccountId == null){ db.logging.update({’_id’:ObjectId(value._id.str)},{$set:{’Properties.AccountId’:value.Properties.EnterpriseId}}, false, true) } });

以上代码一次只更新100条,有时候只更新几百条,20万条的数据一次性更新不完,这是为什么呢?

问题解答

回答1:

有几点不是十分明白:

看起来像是shell脚本对吗?

既然条件中有{'Properties.AccountId':null},为什么还要if(value.Properties.AccountId == null)?或者你想判断的是AccountId === null?

update方法的详细说明可以查看文档。文档中的定义是:db.collection.update(query, update, options),所以不知道最后的false和true本意是想查什么?upsert和multi?这样的话应该是:

db.logging.update({’_id’:ObjectId(value._id.str)},{$set:{’Properties.AccountId’:value.Properties.EnterpriseId}}, {upsert: false, multi: true})

不过你用的是_id条件应该也没有multi什么事。嗯,还是清楚地说下你的本意比较好,我就不猜测了。

你用的是循环的更新,而每一次循环都是带条件的,如果要更新20万条的话,这些条件是不是能覆盖到完整的20万条数据?

相关文章: