Time Complexity of $addToset vs $push when element does not exist in the Array

后端 未结 3 1588
鱼传尺愫
鱼传尺愫 2021-02-06 01:59

Given: Connection is Safe=True so Update\'s return will contain update information.

Say I have a documents that look like:

[{\'a\': [1]}, {\'a\': [2]}, {         


        
3条回答
  •  北海茫月
    2021-02-06 02:30

    Adding my observation in difference between addToSet and push from bulk update of 100k documents.

    when you are doing bulk update. addToSet will be executed separately.

    for example,

    bulkInsert.find({x:y}).upsert().update({"$set":{..},"$push":{ "a":"b" } , "$setOnInsert":  {} })
    

    will first insert and set the document. And then it executes addToSet query.

    I saw clear difference of 10k between

    db.collection_name.count() #gives around 40k 
    
    db.collection_name.count({"a":{$in:["b"]}}) # it gives only around 30k
    

    But when replaced $addToSet with $push. both count query returned same value.

    note: when you're not concerned about duplicate entry in array. you can go with $push.

提交回复
热议问题