MongoDB中的多个$ inc更新 [英] Multiple $inc updates in MongoDB

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

问题描述

是否可以通过在单个更新文档中传递两个$ inc运算符来更新单个文档?

Is it possible to update a single document by passing two $inc operators in a single update document?

例如,我正在尝试使用以下更新文档来增加给定文档中的两个不同字段:

For example, I am trying to increment two different fields in a given document using the following update document:

{
    "$inc" : { "ViewAggregates.4d75b891842f2d3930cf7674" : 1 },
    "$inc" : { "ViewAggregates.Total" : 1 }
}

不会引发任何错误,并且不会更新文档,但是仅增加了一个字段.好像服务器无视第一个$ inc运算符,而实际上仅应用了第二个.

No errors are thrown and the document is updated but only one of the fields has been incremented. It is as if the server disregarded the first $inc operator and only the second was actually applied.

这是否是预期的\正确的行为,还是我想念的东西?

Is this the intended\correct behavior or is there something I am missing?

推荐答案

这是字典键具有唯一性的一个有趣的副作用-第二个$inc覆盖了第一个.

This is an interesting side-effect of dictionary keys being unique -- the second $inc overwrites the first.

但是,仍然可以增加多个字段:

However, it's still possible to increment more than one field:

{
    "$inc": {
        "ViewAggregates.4d75b891842f2d3930cf7674" : 1, 
        "ViewAggregates.Total" : 1
    }
}

这也适用于其他许多运营商:-)

This works for many other operators too :-)

这篇关于MongoDB中的多个$ inc更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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