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

后端 未结 5 1301
爱一瞬间的悲伤
爱一瞬间的悲伤 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:22

    One useful class of use cases is counters and similar cases. For example, take a look at this code (one of the MongoDB tests): find_and_modify4.js.

    Thus, with findAndModify you increment the counter and get its incremented value in one step. Compare: if you (A) perform this operation in two steps and somebody else (B) does the same operation between your steps then A and B may get the same last counter value instead of two different (just one example of possible issues).

提交回复
热议问题