mongodb 单个语句中的多个 mongo 更新运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:33:32  来源:igfitidea点击:

multiple mongo update operator in a single statement?

mongodb

提问by bertie

Can i combine $pushAll and $inc in one statement ?

我可以在一个语句中组合 $pushAll 和 $inc 吗?

Before combine, this works fine :

在结合之前,这很好用:

db.createCollection("test");
db.test.insert({"name" : "albert", "bugs" : []});

db.test.update({"name":"albert"}, 
    {"$pushAll" : {"bugs" : [{"name":"bug1", "count":1}]}});

db.test.update({"name":"albert"}, 
    {"$inc" : {"bugs.0.count" : 1}});

db.test.update({"name":"albert"}, 
    {"$pushAll" : {"bugs" : [{"name":"bug2", "count":1}]}});

But when i try combining it like this :

但是当我尝试像这样组合它时:

db.createCollection("test");
db.test.insert({"name" : "albert", "bugs" : []});

db.test.update({"name":"albert"}, 
    {"$pushAll" : {"bugs" : [{"name":"bug1", "count":1}]}});

db.test.update({"name":"albert"}, 
    {
       "$pushAll" : {"bugs" : [{"name":"bug2", "count":1}]}, 
       "$inc" : {"bugs.0.count" : 1}
    }
);

This error happens :

发生此错误:

have conflicting mods in update

I wonder it is possible to do this, and also, i imagine combining more than just pushAll and inc, but i am not sure whether this is supported or not ?

我想知道是否有可能做到这一点,而且,我想象的不仅仅是 pushAll 和 inc,但我不确定这是否受支持?



update march 23, 2012

2012 年 3 月 23 日更新

I tried multiple $inc on different elements of an array, and it works (although not correctly. quoted from the answer below to clarify why this doensnt work well and what works : The correct way to increment both fields is as follows: > db.test.update({"name":"albert"}, {"$inc" : {"bugs.0.count" : 1, "bugs.1.count" : 1}}):

我在数组的不同元素上尝试了多个 $inc,并且它有效(虽然不正确。从下面的答案中引用以阐明为什么这不起作用以及什么有效The correct way to increment both fields is as follows: > db.test.update({"name":"albert"}, {"$inc" : {"bugs.0.count" : 1, "bugs.1.count" : 1}})::

db.test.update({"name":"albert"}, 
  {
    "$inc" : {"bugs.0.count" : 1}, 
    "$inc" : {"bugs.1.count" : 1}
  }
);

And this combination of $set and $inc on different elements of an array also works :

并且 $set 和 $inc 在数组的不同元素上的这种组合也有效:

db.test.update({"name":"albert"}, 
  {
    "$set" : {"bugs.0.test" : {"name" : "haha"}}, 
    "$inc" : {"bugs.0.count" : 1}, 
    "$inc" : {"bugs.1.count" : 1}
  }
);

But combine any of these with $push or $pushAll, all will go error.

但是将其中任何一个与 $push 或 $pushAll 结合使用,都会出错。

So, my current conclusion is, it is not about multiple operations on multiple elements within the same array which is the problem, but combining these operations with $push or $pushAll that can change the the array is the problem.

所以,我目前的结论是,问题不在于对同一数组中的多个元素进行多次操作,而是将这些操作与可以更改数组的 $push 或 $pushAll 结合起来才是问题所在。

回答by Marc

Multiple updates may be performed on the same document, as long as those updates do not conflict (hence the "have conflicting mods in update" error).

可以对同一文档执行多次更新,只要这些更新不冲突(因此会出现“更新中存在冲突的 mods”错误)。

Because "$push" : {"bugs" : [{"name":"bug1", "count":1}]} and "$inc" : {"bugs.0.count" : 1} are both attempting to modify the same portion of the document (namely the "bugs" array), they conflict.

因为 "$push" : {"bugs" : [{"name":"bug1", "count":1}]} 和 "$inc" : {"bugs.0.count" : 1} 都试图修改文档的相同部分(即“bugs”数组),它们会发生冲突。

Multiple updates may be combined if each affects a different part of the document:

如果每个更新影响文档的不同部分,则可以组合多个更新:

for example:

例如:

> db.test.drop()
true
> db.test.save({ "_id" : 1, "name" : "albert", "bugs" : [ ] })
> db.test.update({"name":"albert"}, {"$pushAll" : {"bugs" : [{"name":"bug1", "count":1}]}, "$inc" : {"increment" : 1}, $set:{"note":"Here is another field."}})
> db.test.find()
{ "_id" : 1, "bugs" : [ { "name" : "bug1", "count" : 1 } ], "increment" : 1, "name" : "albert", "note" : "Here is another field." }
> 

The update contained three different operations ($pushAll, $inc, and $set), but was able to complete successfully, because each operation affected a different part of the document.

更新包含三个不同的操作($pushAll、$inc 和 $set),但能够成功完成,因为每个操作影响文档的不同部分。

I realize this is not exactly what you were hoping to do, but hopefully it will provide you with a little better understanding of how updates work, and perhaps provide some ideas how your updates and/or documents may be restructured to perform the functionality that your application requires. Good luck.

我意识到这并不完全是您希望做的,但希望它能让您更好地了解更新的工作方式,并可能提供一些想法,您的更新和/或文档可以如何重组以执行您的功能应用程序需要。祝你好运。