0

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

5
  • Your syntax is correct. It works on my server too Commented Aug 9, 2018 at 8:42
  • see the console for your id Commented Aug 9, 2018 at 8:52
  • The id is correct ... there must be something else I'm missing Commented Aug 9, 2018 at 9:01
  • can you look at the mongo logs and see the exact query that is executed? Commented Aug 9, 2018 at 10:07
  • Run this command in mongo shell db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } ). Commented Aug 9, 2018 at 18:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And it updated all the status property for all objects?
yes. { "_id" : ObjectId("5b6bfdb818f454e0492896f1"), "codeId" : 1, "generatedCodes" : [ { "name" : "Code 1", "status" : "Validated" }, { "name" : "Code 2", "status" : "Validated" }, { "name" : "Code 3", "status" : "Validated" } ] }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.