3

I try to update a specific object in a document. In this example, I want to change the object with group.id = 'fdsfsFDSFdsfFdsFd' and data.id = 'qqdWSdksFVfSVSSCD'.

That means I want to update the object { "id" : "qqdWSdksFVfSVSSCD", "note 2" : "text" } with var newData = { "id" : "qqdWSdksFVfSVSSCD", "new one" : "anything" }.

{
    "_id" : "wLXDvjDvbsxzfxabR",
    "group" : [
        {
            "id" : "dfDFSfdsFDSfdsFws",
            "title" : "title 1",
            "data" : [
                {
                    "id" : "efBDEWVvfdvsvsdvs",
                    "note" : "text"
                }
            ]
        },
        {
            "id" : "fdsfsFDSFdsfFdsFd",
            "title" : "title 2",
            "data" : [
                {
                    "id" : "WVvfsvVFSDWVDSVsv",
                    "note 1" : "text"
                },
                {
                    "id" : "qqdWSdksFVfSVSSCD",
                    "note 2" : "text"
                },
                {
                    "id" : "MZgsdgtscdvdsRsds",
                    "note 3" : "text"
                }
            ]
        }
    ]
}

So how do I access exactly this object?

Collection.update(
    { _id: 'wLXDvjDvbsxzfxabR' },
    { $set: { group.data: newData } } // group: fdsfsFDSFdsfFdsFd, data: qqdWSdksFVfSVSSCD
)

I don't get it to update a nested array element...

user3142695
  • 15,844
  • 47
  • 176
  • 332

3 Answers3

3

If you want to update a specific element in the array then you will have to include that in your query. Also, when querying in nested objects you should wrap the query in quotes.

// Update element in array with id "dfDFSfdsFDSfdsFws"
Collection.update(
    { _id: "wLXDvjDvbsxzfxabR", "group.id": "dfDFSfdsFDSfdsFws"}, 
    {$set: { "group.data": newData }}
);
David Kleiman
  • 626
  • 7
  • 15
  • But i need two levels. So, how do i access exactly the object i described. The other elements in the document shouldn't be changed. – user3142695 Jan 10 '16 at 06:41
  • Sorry, I misunderstood the question. See Michael Floyd's answer. That is, just change the query `{ _id: "wLXDvjDvbsxzfxabR", "group.id": "dfDFSfdsFDSfdsFws"}` to `{ _id: "wLXDvjDvbsxzfxabR", "group.data.id": "qqdWSdksFVfSVSSCD"}`. – David Kleiman Jan 11 '16 at 02:57
1

David's answer is close:

Collection.update(
  { _id: "wLXDvjDvbsxzfxabR", "group.data.id": "qqdWSdksFVfSVSSCD"}, 
  {$set: { "group.data": newData }}
);
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • i am getting "group.data" is not allowed by the schema? – Gobliins Feb 02 '21 at 14:49
  • That means your using a schema manager (ex: simpl-schema) - check your schema definition. Does the schema allow for a `group` object in `Collection` and does the `group` object include a `data` array? – Michel Floyd Feb 02 '21 at 22:09
  • i use aldeed:simpleschema, and i have an array in my schema wit objects in it. Schema (according to this example) `group: { type: [Object] }, group.$.data: { type: Object }`. But i fugred out with the `$` in between. It works.. – Gobliins Feb 03 '21 at 08:22
1

As mentioned here and here it is impossible to update nested arrays in array right now.

If you can I would recommend you to change you document schema in order to remove embedded array in array. You can change one array to JSON object:

{
"_id" : "wLXDvjDvbsxzfxabR",
"group" : [
    {
        "id" : "dfDFSfdsFDSfdsFws",
        "title" : "title 1",
        "data" : {
             "efBDEWVvfdvsvsdvs": {
                "note" : "text"
             } 
        }
    },
    {
        "id" : "fdsfsFDSFdsfFdsFd",
        "title" : "title 2",
        "data" : {
            "WVvfsvVFSDWVDSVsv": {
                "note 1" : "text"  
            },
            "qqdWSdksFVfSVSSCD": {
                "note 2" : "text"
            },
            "MZgsdgtscdvdsRsds": {
                "note 3" : "text"
            }
        }
    }
]
}

With such schema you can update specific object in data field with such query:

db.collection.update(
  {'group.id': 'fdsfsFDSFdsfFdsFd'}, 
  {$set: {'group.$.data.qqdWSdksFVfSVSSCD': {"new one" : "anything"}}}
)
Community
  • 1
  • 1
Volodymyr Synytskyi
  • 3,885
  • 1
  • 15
  • 19