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...

0

3 Answers 3

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 }}
);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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"}.
1

David's answer is close:

Collection.update(
  { _id: "wLXDvjDvbsxzfxabR", "group.data.id": "qqdWSdksFVfSVSSCD"}, 
  {$set: { "group.data": newData }}
);

3 Comments

i am getting "group.data" is not allowed by the schema?
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?
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..
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"}}}
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.