通过mongo shell更新mongoDB中的嵌套数组 [英] Updating nested arrays in mongoDB via mongo shell

查看:110
本文介绍了通过mongo shell更新mongoDB中的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是MongoDB文档:

Following is a MongoDB document:

{
    "_id" : 2,
    "mem_id" : M002,
    "email" : "xyz@gmail.com",
    "event_type" : [ 
        {
            "name" : "MT",
            "count" : 1,
            "language" : [ 
                {
                    "name" : "English",
                    "count" : 1,
                    "genre" : [ 
                        {
                            "name" : "Action",
                            "count" : 6
                        }, 
                        {
                            "name" : "Sci-Fi",
                            "count" : 3
                        }
                    ],
                    "cast" : [ 
                        {
                            "name" : "Sam Wortington",
                            "count" : 2
                        }, 
                        {
                            "name" : "Bruce Willis",
                            "count" : 4
                        }, 
                        {
                            "name" : "Will Smith",
                            "count" : 7
                        }, 
                        {
                            "name" : "Irfan Khan",
                            "count" : 1
                        }
                    ]
                }
            ]
        }
    ]
}

由于嵌套,我无法更新数组类型的字段,特别是event_type,语言,体裁和演员表.基本上,我想更新所有提到的四个字段以及每个和子文档的计数字段.如果值是新的,则update语句应在树中插入一个值,否则应增加该值的计数.
mongo shell中的查询可以是什么? 谢谢

I'm not able to update fields that is of type array, specially event_type, language, genre and cast because of nesting. Basically, I wanted to update all the four mentioned fields along with count field for each and subdocuments. The update statement should insert a value to the tree if the value is new else should increment the count for that value.
What can be the query in mongo shell? Thanks

推荐答案

您直接遇到了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>}})

这篇关于通过mongo shell更新mongoDB中的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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