gpt4 book ai didi

mongodb - MongoDB Update() 方法如何工作?

转载 作者:可可西里 更新时间:2023-11-01 09:44:11 26 4
gpt4 key购买 nike

考虑 mycol 收集器具有以下数据。

{ "_id" : ObjectId("544946347db27ca99e20a95f"), "title" : "MongoDB Overview" }
{ "_id" : ObjectId("544946357db27ca99e20a960"), "title" : "MongoDB Overview" }
{ "_id" : ObjectId("544946377db27ca99e20a961"), "title" : "MongoDB Overview" }

让我们更新文档。

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})

这会导致文档更新。

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find();

现在,当我尝试在我的 collection 中查找文档时,我看到第一个文档正在更新。当存在文档重复时,我对 更新在 mongodb 中的工作方式 感到困惑。

{ "_id" : ObjectId("544946347db27ca99e20a95f"), "title" : "New MongoDB Tutorial" }
{ "_id" : ObjectId("544946357db27ca99e20a960"), "title" : "MongoDB Overview" }
{ "_id" : ObjectId("544946377db27ca99e20a961"), "title" : "MongoDB Overview" }

最佳答案

当多个文档匹配 update 条件时,只有 第一个 匹配的文档被更新,除非 multi使用选项:

db.mycol.update(
{'title': 'MongoDB Overview'},
{$set: {'title': 'New MongoDB Tutorial'}},
{multi: true})

关于mongodb - MongoDB Update() 方法如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26534719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com