heterogeneous bulk update in mongodb

前端 未结 3 1942
甜味超标
甜味超标 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.

提交回复
热议问题