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

187
Views
Mongodb positional operator is not working

When I am trying to update just "title", it is working fine, but if I am trying to update the nested object in the array, it is not working.

Data: Data

Working:

const restaurant = await Restaurant.update(
    {_id: '60e6828e4992a2979fa0ba3e'},
    {$set: {title: "Hello}},
  );

Not working:

const restaurant = await Restaurant.update(
    {_id: '60e6828e4992a2979fa0ba3e', 'grades.grade': 85},
    {$set: {'grades.$.grade': 0}},
  );
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Demo - https://mongoplayground.net/p/caFBz-lx8dQ

Use $[]

The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update operation

db.collection.update(
 {},
 { $set: { "grades.$[elem].grade": 0 }},
 { arrayFilters: [ {"elem.grade": 85 } ]}
)
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!