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

97
Views
Find and Update nested array element by ID

I have a user model something like this

{
     _id:"something",
     email:"abc@gmail.com",
     contacts:[
     {_id:"123",status:"PENDING"},
     {_id:"456",status:"PENDING"},
     {_id:"789",status:"PENDING"}
     ]
}

And I have an id of one of the user's contacts "123" and I want to update that specific contact's status from "PENDING" to "SUCCESSFUL". How can I do that?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Easiest way is with the array identifier $, like so:

db.collection.updateOne({
  email: "abc@gmail.com",
  "contacts._id": "123"
},
{
  "$set": {
    "contacts.$.status": "SUCCESSFUL"
  }
})

Mongo Playground

The array identifier updates the first element of the array that matches the query.

about 4 years ago · Juan Pablo Isaza Report

0

db.collection.updateOne({ _id: "something" }, { contacts: [{ _id: "123", status: "SUCCESSFUL" }] }, function (err, res) {
    
})
about 4 years ago · Juan Pablo Isaza 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!