简体   繁体   English

MongoDB更新多个数组项

[英]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: 我正在尝试通过使用以下代码来更新每个对象的status属性:

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

But none of the array items get the new status ... 但是没有一个数组项获得新的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} 与您的代码唯一的区别是我将{codeId:id}更改为{codeId:1}
so make sure you are passing the correct id. 因此,请确保您传递正确的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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