What's the difference between findAndModify and update in MongoDB?

后端 未结 5 1297
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 10:09

I\'m a little bit confused by the findAndModify method in MongoDB. What\'s the advantage of it over the update method? For me, it seems that it jus

5条回答
  •  北海茫月
    2020-11-27 10:42

    If you fetch an item and then update it, there may be an update by another thread between those two steps. If you update an item first and then fetch it, there may be another update in-between and you will get back a different item than what you updated.

    Doing it "atomically" means you are guaranteed that you are getting back the exact same item you are updating - i.e. no other operation can happen in between.

提交回复
热议问题