MongoDB的:在更新数组的数组 [英] MongoDB: updating an array in array
问题描述
我似乎有访问嵌套在MongoDB的文档中的数组中的数组内容的问题。我没有问题访问第一阵列组与类似下面的查询...
I seem to be having an issue accessing the contents of an array nested within an array in a mongodb document. I have no problems accessing the first array "groups" with a query like the following...
db.orgs.update({_id: org_id, "groups._id": group_id} , {$set: {"groups.$.name": "new_name"}});
我在哪里碰到麻烦的是,当我试图修改数组中的元素的性质特征嵌套在组数组中。
Where I run into trouble is when I try to modify properties of an element in the array "features" nested within the "group" array.
下面是一个例子文档的样子
Here is what an example document looks like
{
"_id" : "v5y8nggzpja5Pa7YS",
"name" : "Example",
"display_name" : "EX1",
"groups" : [
{
"_id" : "s86CbNBdqJnQ5NWaB",
"name" : "Group1",
"display_name" : "G1",
"features" : [
{
_id : "bNQ5Bs8BWqJn6CdNa"
type : "blog",
name : "[blog name]"
owner_id : "ga5YgvP5yza7pj8nS"
},
]
},
]
},
这是我尝试使用该查询。
And this is the query I tried to use.
db.orgs.update({_id: "v5y8nggzpja5Pa7YS", "groups._id": "qBX3KDrtMeJGvZWXZ", "groups.features._id":"bNQ5Bs8BWqJn6CdNa" }, {$set: {"groups.$.features.$.name":"New Blog Name"}});
它返回的错误信息:
It returns with an error message:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "Too many positional (i.e. '$') elements found in path 'groups.$.features.$.name'"
}
})
似乎蒙戈不支持通过位置元件修改嵌套数组内的数组?
It seems that mongo doesn't support modifying arrays nested within arrays via the positional element?
有没有修改这个数组没有考虑整个事情出来,修改它,然后把它放回的方法吗?有了这样多重嵌套是标准的做法,以创建一个新的收藏? (即使当父数据是必要的仅曾经需要的数据)应我通过键改变文档结构,使得第二嵌套阵列是一个对象,并获得了吗? (其中最关键的是,可以作为一个_id行为的整数值)
Is there a way to modify this array without taking the entire thing out, modifying it, and then putting it back in? With multiple nesting like this is it standard practice to create a new collection? (Even though the data is only ever needed when the parent data is necessary) Should I change the document structure so that the second nested array is an object, and access it via key? (Where the key is an integer value that can act as an "_id")
groups.$.features.[KEY].name
什么被认为是正确的方式做到这一点?
What is considered the "correct" way to do this?
推荐答案
一些调查研究后,它看起来像只有这样,才能修改阵列中的阵列将与一些外部逻辑来找到我想要的元素的索引改变。这样做将要求每一个变化为具有查找查询来定位索引,然后更新查询修改阵列。这似乎不是最好的方法。
After some more research, it looks like the only way to modify the array within an array would be with some outside logic to find the index of the element I want to change. Doing this would require every change to have a find query to locate the index, and then an update query to modify the array. This doesn't seem like the best way.
由于我将永远知道该功能的ID,我都选择修改我的文档结构。
Since I will always know the ID of the feature, I have opted to revise my document structure.
{
"_id" : "v5y8nggzpja5Pa7YS",
"name" : "Example",
"display_name" : "EX1",
"groups" : [
{
"_id" : "s86CbNBdqJnQ5NWaB",
"name" : "Group1",
"display_name" : "G1",
"features" : {
"1" : {
type : "blog",
name : "[blog name]"
owner_id : "ga5YgvP5yza7pj8nS"
},
}
},
]
},
使用了新的结构,改变可以以下列方式制成:
With the new structure, changes can be made in the following manner:
db.orgs.update({_id: "v5y8nggzpja5Pa7YS", "groups._id": "s86CbNBdqJnQ5NWaB"}, {$set: {"groups.$.features.1.name":"Blog Test 1"}});
这篇关于MongoDB的:在更新数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!