mongodb 执行条数不确定问题
为情所困
为情所困 2017-05-17 10:01:33
0
1
737
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)
左手右手慢动作

有几点不是十分明白:

  1. 看起来像是shell脚本对吗?

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

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

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

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!