问题描述
我可以在一个语句中组合 $pushAll 和 $inc 吗?
Can i combine $pushAll and $inc in one statement ?
在结合之前,这很好用:
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}
}
);
发生此错误:
have conflicting mods in update
我想知道是否有可能做到这一点,而且,我想象的不仅仅是 pushAll 和 inc,但我不确定这是否受支持?
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 ?
我在数组的不同元素上尝试了多个 $inc,并且它有效(虽然不正确.从下面的答案中引用以阐明为什么这不起作用以及什么有效:增加两个字段的正确方法是如下: > db.test.update({"name":"albert"}, {"$inc" : {"bugs.0.count" : 1, "bugs.1.count" : 1}})代码>:
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}})
:
db.test.update({"name":"albert"},
{
"$inc" : {"bugs.0.count" : 1},
"$inc" : {"bugs.1.count" : 1}
}
);
并且 $set 和 $inc 在数组的不同元素上的这种组合也有效:
And this combination of $set and $inc on different elements of an array also works :
db.test.update({"name":"albert"},
{
"$set" : {"bugs.0.test" : {"name" : "haha"}},
"$inc" : {"bugs.0.count" : 1},
"$inc" : {"bugs.1.count" : 1}
}
);
但是将其中任何一个与 $push 或 $pushAll 结合使用,都会出错.
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.
推荐答案
可以对同一个文档执行多次更新,只要这些更新不冲突(因此会出现更新中存在冲突的 mods"错误).
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).
因为 "$push" : {"bugs" : [{"name":"bug1", "count":1}]} 和 "$inc" : {"bugs.0.count" : 1} 是两者都试图修改文档的同一部分(即错误"数组),它们发生冲突.
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.
如果每个更新影响文档的不同部分,则可以组合多个更新:
Multiple updates may be combined if each affects a different part of the document:
例如:
> 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." }
>
更新包含三个不同的操作($pushAll、$inc 和 $set),但能够成功完成,因为每个操作影响文档的不同部分.
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.
我意识到这并不完全是您希望做的,但希望它能让您更好地了解更新的工作原理,并可能提供一些想法,您的更新和/或文档可能会被重组以执行您的应用程序需要的功能.祝你好运.
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.
这篇关于单个语句中的多个 mongo 更新运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!