heterogeneous bulk update in mongodb

前端 未结 3 1943
甜味超标
甜味超标 2021-02-14 22:58

I know that we can bulk update documents in mongodb with

db.collection.update( criteria, objNew, upsert, multi )

in one db call, but it\'s homo

相关标签:
3条回答
  • 2021-02-14 23:07

    There is no real benefit from doing updates the way you suggest.

    The reason that there is a bulk insert API and that it is faster is that Mongo can write all the new documents sequentially to memory, and update indexes and other bookkeeping in one operation.

    A similar thing happens with updates that affect more than one document: the update will traverse the index only once and update objects as they are found.

    Sending multiple criteria with multiple criteria cannot benefit from any of these optimizations. Each criteria means a separate query, just as if you issued each update separately. The only possible benefit would be sending slightly fewer bytes over the connection. The database would still have to do each query separately and update each document separately.

    All that would happen would be that Mongo would queue the updates internally and execute them sequentially (because only one update can happen at any one time), this is exactly the same as if all the updates were sent separately.

    It's unlikely that the overhead in sending the queries separately would be significant, Mongo's global write lock will be the limiting factor anyway.

    0 讨论(0)
  • 2021-02-14 23:13

    we are seeing some benefits of $in clause. our use case was to update the 'status' in a document for a large number number records. In our first cut, we were doing a for loop and doing updates one by 1. But then we switched to using $in clause and that made a huge improvement.

    0 讨论(0)
  • 2021-02-14 23:18

    That's two seperate questions. To the first one; there is no MongoDB native mechanism to bulk send criteria/update pairs although technically doing that in a loop yourself is bound to be about as efficient as any native bulk support.

    Checking for the existence of a document based on an embedded document (what you refer to as compound key, but in the interest of correct terminology to avoid confusion it's better to use the mongo name in this case) and insert/update depending on that existence check can be done with upsert :

    document A :

    {
        _id: ObjectId(...),
        key: {
            name: "Will",
            age: 20
        }
    }
    
    db.users.update({name:"Will", age:20}, {$set:{age: 21}}), true, false)
    

    This upsert (update with insert if no document matches the criteria) will do one of two things depending on the existence of document A :

    • Exists : Performs update "$set:{age:21}" on the existing document
    • Doesn't exist : Create a new document with fields "name" and field "age" with values "Will" and "20" respectively (basically the criteria are copied into the new doc) and then the update is applied ($set:{age:21}). End result is a document with "name"="Will" and "age"=21.

    Hope that helps

    0 讨论(0)
提交回复
热议问题