MongoDB中的findAndModify和update之间有什么区别? [英] What's the difference between findAndModify and update in MongoDB?

查看:464
本文介绍了MongoDB中的findAndModify和update之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MongoDB中的findAndModify方法有些困惑.与update方法相比,它的优点是什么?对我来说,它似乎只是先返回该项目,然后再对其进行更新.但是为什么我需要先退货?我阅读了 MongoDB:权威指南 ,它说它很方便处理队列和执行其他需要获取和设置样式原子性的操作.但是我不明白它是如何实现的.有人可以向我解释吗?

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 just returns the item first and then updates it. But why do I need to return the item first? I read the MongoDB: the definitive guide and it says that it is handy for manipulating queues and performing other operations that need get-and-set style atomicity. But I didn't understand how it achieves this. Can somebody explain this to me?

推荐答案

如果您获取一个项目然后对其进行更新,则在这两个步骤之间可能存在另一个线程进行的更新.如果先更新一个项目然后取回它,则它们之间可能还会有另一个更新,并且您将获得与更新的项目不同的项目.

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.

这篇关于MongoDB中的findAndModify和update之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆