3

I have the below document which contains double nested array format. I have to update the "level" field to "Senior Engineer" when the "someKey":"somevalue" and "Company":"Company1" and "Name":"Nandhi".

Document

    { 
        "_id" : "777", 
        "someKey" : "someValue", 
        "someArray" : [
            {
                "Company" : "Company1", 
                "someNestedArray" : [
                    {
                        "name" : "Nandhi", 
                        "level" : "Junior Engineer"
                    }, 
                    {
                        "name" : "Rajan", 
                        "level" : "Senio Engineer"
                    }
                ]
            }], 
            {
                "Company" : "Company2", 
                "someNestedArray" : [
                    {
                        "name" : "Nandhi", 
                        "level" : "Junior Engineer"
                    }, 
                    {
                        "name" : "Rajan", 
                        "level" : "Senio Engineer"
                    }
                ]
            }
        ]
    }

Update Query I tried

    db.Test123.updateOne(
    {"someKey" : "someValue","someArray.Company":"Company1"},
    {$set:{"someArray.$[someNestedArray].level":"Senior Developer"}},
    {arrayFilters:[{"someNestedArray.name":"Nandhi"}]}
    );

Output Screenshot

enter image description here

As you can seen that, the modifiedCount returns 0. Please advice on this!

1 Answer 1

3

You need to define arrayFilter for every level of nesting, try:

db.Test123.update(
    { "someKey" : "someValue" },
    { "$set": { "someArray.$[someArrayDoc].someNestedArray.$[someNestedArrayDoc].level": "Senior Developer" } },
    { arrayFilters: [ {"someArrayDoc.Company": "Company1"}, { "someNestedArrayDoc.name": "Nandhi" } ] }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.