MongoDB中findAndModify和update的区别是什么?

时间:2022-10-25 21:08:15

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?

我对MongoDB中的findAndModify方法有点迷惑。更新方法的优点是什么?对我来说,它似乎是先返回项目,然后更新它。但是为什么我需要先返回项目呢?我读了MongoDB:权威指南,它说它对于操作队列和执行需要get- set风格原子性的其他操作非常方便。但我不明白它是怎么做到的。有人能给我解释一下吗?

4 个解决方案

#1


124  

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.

“原子化”的做法意味着你得到保证,你得到的是你正在更新的完全相同的东西——也就是说,在这两者之间不可能发生其他操作。

#2


38  

findAndModify returns the document, update does not.

findAndModify返回文档,更新不返回。

If I understood Dwight Merriman (one of the original authors of mongoDB) correctly, using update to modify a single document i.e.("multi":false} is also atomic. Currently, it should also be faster than doing the equivalent update using findAndModify.

如果我正确地理解了Dwight Merriman (mongoDB的原始作者之一),使用update修改单个文档,即(“多”:假}也是原子。目前,它还应该比使用findAndModify进行等效更新更快。

#3


19  

From the MongoDB docs (emphasis added):

来自MongoDB文档(重点补充):

  • By default, both operations modify a single document. However, the update() method with its multi option can modify more than one document.

    默认情况下,两个操作都修改一个文档。但是,带有多选项的update()方法可以修改多个文档。

  • If multiple documents match the update criteria, for findAndModify(), you can specify a sort to provide some measure of control on which document to update. With the default behavior of the update() method, you cannot specify which single document to update when multiple documents match.

    如果多个文档匹配更新标准,对于findAndModify(),您可以指定一个排序来提供一些控制文档更新的方法。使用update()方法的默认行为,当多个文档匹配时,您无法指定要更新哪一个文档。

  • By default, findAndModify() method returns the pre-modified version of the document. To obtain the updated document, use the new option. The update() method returns a WriteResult object that contains the status of the operation. To return the updated document, use the find() method. However, other updates may have modified the document between your update and the document retrieval. Also, if the update modified only a single document but multiple documents matched, you will need to use additional logic to identify the updated document.

    默认情况下,findAndModify()方法返回文档的预修改版本。要获取更新的文档,请使用新选项。update()方法返回一个包含操作状态的WriteResult对象。要返回更新的文档,请使用find()方法。但是,其他更新可能已经修改了更新和文档检索之间的文档。另外,如果更新只修改了单个文档,但是匹配了多个文档,则需要使用额外的逻辑来标识更新后的文档。

  • You cannot specify a write concern to findAndModify() to override the default write concern whereas, starting in MongoDB 2.6, you can specify a write concern to the update() method.

    您不能指定一个写关注点来找到findAndModify()来覆盖默认的写关注点,而从MongoDB 2.6开始,您可以指定update()方法的写关注。

When modifying a single document, both findAndModify() and the update() method atomically update the document.

在修改单个文档时,findAndModify()和update()方法会原子地更新文档。

#4


8  

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.

一个有用的用例类是计数器和类似的例子。例如,看看这段代码(MongoDB测试之一):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).

因此,在findAndModify中增加计数器并在一个步骤中得到它的递增值。比较:如果您(A)在两个步骤中执行这个操作,而另一个人(B)在您的步骤中执行相同的操作,那么A和B可能会得到相同的最后一个计数器值,而不是两个不同的值(只是一个可能的问题的示例)。

#1


124  

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.

“原子化”的做法意味着你得到保证,你得到的是你正在更新的完全相同的东西——也就是说,在这两者之间不可能发生其他操作。

#2


38  

findAndModify returns the document, update does not.

findAndModify返回文档,更新不返回。

If I understood Dwight Merriman (one of the original authors of mongoDB) correctly, using update to modify a single document i.e.("multi":false} is also atomic. Currently, it should also be faster than doing the equivalent update using findAndModify.

如果我正确地理解了Dwight Merriman (mongoDB的原始作者之一),使用update修改单个文档,即(“多”:假}也是原子。目前,它还应该比使用findAndModify进行等效更新更快。

#3


19  

From the MongoDB docs (emphasis added):

来自MongoDB文档(重点补充):

  • By default, both operations modify a single document. However, the update() method with its multi option can modify more than one document.

    默认情况下,两个操作都修改一个文档。但是,带有多选项的update()方法可以修改多个文档。

  • If multiple documents match the update criteria, for findAndModify(), you can specify a sort to provide some measure of control on which document to update. With the default behavior of the update() method, you cannot specify which single document to update when multiple documents match.

    如果多个文档匹配更新标准,对于findAndModify(),您可以指定一个排序来提供一些控制文档更新的方法。使用update()方法的默认行为,当多个文档匹配时,您无法指定要更新哪一个文档。

  • By default, findAndModify() method returns the pre-modified version of the document. To obtain the updated document, use the new option. The update() method returns a WriteResult object that contains the status of the operation. To return the updated document, use the find() method. However, other updates may have modified the document between your update and the document retrieval. Also, if the update modified only a single document but multiple documents matched, you will need to use additional logic to identify the updated document.

    默认情况下,findAndModify()方法返回文档的预修改版本。要获取更新的文档,请使用新选项。update()方法返回一个包含操作状态的WriteResult对象。要返回更新的文档,请使用find()方法。但是,其他更新可能已经修改了更新和文档检索之间的文档。另外,如果更新只修改了单个文档,但是匹配了多个文档,则需要使用额外的逻辑来标识更新后的文档。

  • You cannot specify a write concern to findAndModify() to override the default write concern whereas, starting in MongoDB 2.6, you can specify a write concern to the update() method.

    您不能指定一个写关注点来找到findAndModify()来覆盖默认的写关注点,而从MongoDB 2.6开始,您可以指定update()方法的写关注。

When modifying a single document, both findAndModify() and the update() method atomically update the document.

在修改单个文档时,findAndModify()和update()方法会原子地更新文档。

#4


8  

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.

一个有用的用例类是计数器和类似的例子。例如,看看这段代码(MongoDB测试之一):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).

因此,在findAndModify中增加计数器并在一个步骤中得到它的递增值。比较:如果您(A)在两个步骤中执行这个操作,而另一个人(B)在您的步骤中执行相同的操作,那么A和B可能会得到相同的最后一个计数器值,而不是两个不同的值(只是一个可能的问题的示例)。