Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Update nested document with mutiple conditions
{
    id: 1,
    name: "venkat",
    description: "Description",
    address: "chennai",
    pinCode: "600100",
    authenticationCode: "123",
    authenticated: false,
    deleted: false,
    Location: [
        {
            name: "foo",
            description: "LocationDecription",
            parent: "parent",
            UserAccess: [
                {
                    loginName: "paklon",
                    role: "role"
                }
            ]
        }
    ]
}

I need to update role:"admin" where conditions match like id: 1, Location's array name: "foo" and Location nested array of "UserAccess.loginName": "paklon" and then update the role.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

According to above mentioned description as a solution to it please try executing following update operation in MongoDB shell

db.collection.update({
    "id": 1,
    "Location": {
        $elemMatch: {
            "name": "foo",
            "UserAccess": {
                $elemMatch: {
                    "loginName": "paklon"
                }
            }
        }
    }
}, {
    $set: {
        "Location.$.UserAccess.0.role": "admin"
    }
})
over 4 years ago · Santiago Trujillo Report

0

The following query should work:

db.collection.update(
    {"id": 1, "Location.name": "foo", "Location.UserAccess.loginName": "paklon"}, 
    {$set: {"Location.0.UserAccess.0.role": "admin"}}
)

To update a particular element in an array you need to use positional operator "$" that actually a placeholder for the first element. So it could be replaced with "0" and as it permitted using the "$" only once in the query, you will have to use "0" at the end of the query in any case.

Also take a look at a corresponding mongodb documentation

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!