2

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.

1 Answer 1

1

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

playground

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