更新MongoDb中的丰富记录 [英] Updating a deep record in MongoDb

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

问题描述

我在MongoDb中有此记录,并且正在使用本机API:

I have this record in MongoDb, and am using the native API:

{
    "_children" : {
        "addressesR" : [
            {
                "id" : ObjectId("530eea01071bd1a53065c1a6"),
                "personId" : ObjectId("530eea01071bd1a53065c1a4"),
                "street" : "ivermey",
                "city" : "perth",
                "_children" : {
                }
            },
            {
                "_children" : {
                    "configId" : {
                        "a" : {
                            "_children" : [
                                {
                                    "b" : 10
                                },
                                {
                                    "b" : 20
                                }
                            ]
                        }
                    }
                },
                "city" : "perth",
                "configId" : ObjectId("530eea01071bd1a53065c1a3"),
                "id" : ObjectId("530eea01071bd1a53065c1a5"),
                "personId" : ObjectId("530eea01071bd1a53065c1a4"),
                "street" : "bitton"
            }
        ],
    }
}

我需要在一个查询中将嵌套的'b'更新为30. 我可以找到记录:

I need to update, in one query, that nested 'b' to 30. I can find the record:

db.peopleR.find( { '_children.addressesR._children.configId.a._children.b': 20 } );

但是我很难找到一种方法来更新该特定值.

But I am having a hard time finding a way to update that particular value.

我正在尝试:

db.peopleR.update( { '_children.addressesR._children.configId.a._children.b': 20 }, { $set: { '_children.addressesR.$._children.configId.a.$._children.b': 30 }   }  )

但是我得到:

Cannot apply the positional operator without a corresponding query field containing an array.

现在,考虑到其他约束,我绝对需要更新 just 'b',我有办法吗?还是不可能两次使用$操作数? (也就是说,如果对象的内部只有1级向下,我才能更新它的内部?)

Now, considering that for other constraints I absolutely need to update just 'b', is there a way for me to do it? Or, is it impossible to use the $ operand twice? ( that is, I can only update the inner part of an object if it's only 1 level down?)

推荐答案

问题是您不能使用$运算符在数组的嵌套级别上显示匹配项.而且最重要的是,您只能指定一次.

The problem is that you cannot use the $ operator to show matching at nested levels in an array. And most importantly you can only specify once.

文档中所述,与找到的第一个元素匹配.这不是,这意味着如果查询有多个匹配项,则只会选择第一个匹配项,这还意味着 第一个匹配项数组位置将被使用.这意味着类似的事情也将不起作用:

As stated from the documentation, is that the $ will only match the first element found. That not only means if you had multiple matches for your query then only the first would be picked, it also means that only the first matched array position will be used. That means something like this would also not work:

{ $set: { '_children.addressesR.1._children.configId.a._children.$.b': 30 } 

因为那样会错误地匹配index 1值并更新您不期望的项目.

As that would erroneously match the index 1 value and update an item you were not expecting.

如果您可以更新整个数组,则可以进行以下操作:

If you could possibly update the whole array then the following would work:

{ $set: {"_children.addressesR.$._children.configId.a._children": [ { b: 10}, { b: 30} ] } }

正如我所说,因为index first 数组匹配.

That is as I said, because the index is matching for the first array.

但是,鉴于您拥有的结构,您最好的选择似乎是将顶级数组更改为子文档.除非实际上有理由将其作为数组,而不是现在看来是两种混合文档类型.

But given the structure that you have, it would seem your best option is to change that top level array to be a subdocument instead. Unless in actuality this has a reason to be an array, rather than the two mixed document types that it appears to be now.

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

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