Valip
Valip

Reputation: 4610

MongoDB update multiple array items

I have a document with this structure:

{
  codeId: 1,
  generatedCodes: [
    {
      name: 'Code 1',
      status: 'In Progress'
    },
    {
      name: 'Code 2',
      status: 'In Progress'
    },
    {
      name: 'Code 3',
      status: 'In Progress'
    }
  ]
}

I'm trying to update the status property of each object by using the following code:

db.codes.update({codeId: id}, {$set: {'generatedCodes.$[].status': 'Validated'}}, {multi: true})

But none of the array items get the new status...

Upvotes: 0

Views: 73

Answers (1)

Eli Skoran
Eli Skoran

Reputation: 278

i just run the following and it worked:

db.getCollection('yourCollection').update({codeId: 1}, {$set: {'generatedCodes.$[].status': 'Validated'}}, {multi: true})

the only difference from your code is that i changed {codeId: id} to {codeId: 1}
so make sure you are passing the correct id.

Upvotes: 1

Related Questions