一个语句中有多个mongo更新运算符? [英] multiple mongo update operator in a single statement?

查看:49
本文介绍了一个语句中有多个mongo更新运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在一个语句中组合$ 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,并且它可以工作(尽管不正确.引用下面的答案来阐明为什么这个无效的工作原理是什么:The correct way to increment both fields is as follows: > 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或$ push组合在一起才是问题所在.

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.

推荐答案

可以在同一文档上执行多个更新,只要这些更新不冲突(因此出现更新时有冲突的mod"错误).

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更新运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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