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

128
Views
How to set values of nested array of objects in mongodb

I have a data model which looks like this, so each documents has services array and each service contains an items array and I want to update properties in items array.

{
    services: [
        {
            id: '1',
            name: 'Service 01',
            items: [
                {
                    id: '1',
                    name: '',
                    qty: 10
                },
                {
                    id: '2',
                    name: '',
                    qty: 10
                },
            ]
        },
        {
            id: '2',
            name: 'Service 02',
            items: [
                {
                    id: '3',
                    name: '',
                    qty: 10
                },
                {
                    id: '4',
                    name: '',
                    qty: 10
                },
            ]
        },
    ]
}

I want to set all the quantities inside services -> items to 0, What will be query for doing it I tried to do,

updateMany({}, { $set: { 'services.items.qty': 0 } });

but it's not working. Let me know if you need more details.

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

0

the all positional operator $[] operator can be used to update all elements

playground

db.collection.update({},
{
  $set: {
    "services.$[].items.$[].qty": 0
  }
})
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!