简体   繁体   中英

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 ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM