search

Home  >  Q&A  >  body text

The problem of uncertain number of execution items in mongodb

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)
       }
    }
);

The above code only updates 100 items at a time, sometimes only a few hundred items are updated, and 200,000 items of data cannot be updated at one time. Why is this?

为情所困为情所困2742 days ago773

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-05-17 10:03:33

    There are a few things that I don’t quite understand:

    1. Looks like a shell script, right?

    2. Since there is {"Properties.AccountId":null},为什么还要if(value.Properties.AccountId == null)?或者你想判断的是AccountId === null?

    3. in the conditions
    4. update方法的详细说明可以查看文档。文档中的定义是:db.collection.update(query, update, options),所以不知道最后的falsetrue本意是想查什么?upsertmulti?This should be:

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

      But what are you using? Well, it's better to state your original intention clearly, so I won't guess. _id条件应该也没有multi

    5. You are using a loop update, and each loop has conditions. If you want to update 200,000 pieces of data, can these conditions cover the complete 200,000 pieces of data?

    6. reply
      0
  • Cancelreply