且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过mongo shell更新mongoDB中的嵌套数组

更新时间:2023-10-02 23:18:22

您直接遇到了MongoDB当前的限制之一. 问题在于引擎不支持多个位置运算符. 参见将位置$操作符多次用于更新嵌套数组

You are directly hitting one of the current limitations of MongoDB. The problem is that the engine does not support several positional operators. See this Multiple use of the positional `$` operator to update nested arrays

为此有一张公开票: https://jira.mongodb.org/browse/SERVER-831 (在此也有提及)

There is an open ticket for this: https://jira.mongodb.org/browse/SERVER-831 (mentioned also there)

您还可以阅读有关如何更改数据模型的内容:更新mongodb中的嵌套数组

You can also read this one on how to change your data model: Updating nested arrays in mongodb

如果您认为可行,则可以执行以下操作:

If it is feasible for you, you can do:

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.0.language.$.count":<number>}})

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.$.language.0.count":<number>}})

但是你不能做:

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.$.language.$.count":<number>}})