简体   繁体   English

MongoDB-更新对象数组中的数组

[英]MongoDB - update arrays in object array

I have the following structure: 我有以下结构:

    {
      data: [
        {pos:"0",
         moreData: ["a", "b"]
        },
        {pos:"1",
         moreData: ["a", "c"]
        },
    ]}

I would like to update the above structure with an update. 我想通过更新来更新上述结构。 I would like to add a letter to moreData where pos=1. 我想给moreData添加一个字母,其中pos = 1。 This is how it should look like at the end: 最后是这样的:

    {
      data: [
        {pos:"0",
         moreData: ["a", "b"]
        },
        {pos:"1",
         moreData: ["a", "c", "d"]
        },
    ]}

I am doing this, at the moment: (using mongoose, but whatever would be fine) 目前,我正在这样做:(使用猫鼬,但可以的)

    model.update({"data.pos":"1"},
                 { $push: {data:{ pos:"1", moreData:["d"] } } },
                 { safe: true, upsert: false }, function(err){
    });

All I'm getting is a duplicated "pos":"1" with a "moreData" containing only the new element "d". 我得到的只是一个重复的“ pos”:“ 1”和一个“ moreData”,其中仅包含新元素“ d”。 Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!

There's something wrong with your data structure, {"data.pos": 1} selects the whole document (so $push doesn't act as expected): 您的数据结构有问题, {"data.pos": 1}选择了整个文档(因此$ push不能按预期工作):

> db.demo.find({"data.pos": "1"})
{ "_id" : ObjectId("533a850fd15748cc2dc1a17f"), "data" : [  {   "pos" : "0",    "moreData" : [  "a",    "b" ] },    {   "pos" : "1",    "moreData" : [  "a",    "c" ] } ] }

Try adding an id or something to your document: 尝试向您的文档添加ID或其他内容:

> db.demo.find({"id": "foo"})
{ "_id" : ObjectId("533a85c9d15748cc2dc1a180"), "id" : "foo", "data" : [    {   "pos" : "0",    "moreData" : [  "a",    "b" ] },    {   "pos" : "1",    "moreData" : [  "a",    "c" ] } ] }
> db.demo.update({"id": "foo"}, {"$push": { "data" : { "pos": "2", "moreData": ["c", "d"]}}})
> db.demo.find({"id": "foo"})
{ "_id" : ObjectId("533a85c9d15748cc2dc1a180"), "data" : [  {   "pos" : "0",    "moreData" : [  "a",    "b" ] },    {   "pos" : "1",    "moreData" : [  "a",    "c" ] },    {   "pos" : "2",    "moreData" : [  "c",    "d" ] } ], "id" : "foo" }

Seems to do what you want. 似乎可以做您想要的。

The problem here is you need to push to the correct array element. 这里的问题是您需要推送到正确的数组元素。 You can either do this directly by dot notation and index value or by using the positional $ operator: 您可以直接通过点表示法和索引值或通过使用位置$运算符来执行此操作:

 model.update(
     {"data.pos": "1" },
     { "$push": { "data.$.moreData": "d" } },
     function(err, numAffected) {

 })

That should match the selected index of the "data" array in order to perform the update. 这应该与“数据”数组的选定索引匹配,以便执行更新。 Beware though, you have nested arrays and you would not be able to match and update an item in the inner array. 但是要当心,您有嵌套的数组,并且您将无法匹配和更新内部数组中的项目。 See the positional operator documentation for this 有关此信息,请参见位置操作员文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM