0

How can i update values in array inside an object in an array.

{
 "_id" : ObjectId("63c7ca9584535c160ee4aaed"),
 "status" : "REJECTED",
 "steps" : [
 {
 "_id" : ObjectId("63c7ca9884535c160ee4ab03"),
 "status" : "REJECTED",
 "stepUsers" : [
 {
 "_id" : ObjectId("63c7ca9884535c160ee4ab04"),
 "status" : "REJECTED",
 }
 ]
 },
 ]
}

I tried to update using arrayFilters but that didn't work. Mongo throw an error MongoServerError: Found multiple array filters with the same top-level field name steps

 Collection.updateOne({
 _id: id
 }, {
 $set: {
 "steps.$[steps].stepUsers.$[stepUsers].status": 'PENDING',
 }
 }, {
 arrayFilters: [
 { "steps._id": step._id },
 { "steps.stepUsers._id": stepUser._id }
 ]
 }) 

I need to update steps.stepUsers.status in the collection.

nimrod serok
16.1k2 gold badges15 silver badges36 bronze badges
asked Feb 2, 2023 at 12:13

1 Answer 1

1

Try to change the arrayFilters: "steps.stepUsers._id" -> "stepUsers._id"

since arrayFilters is referencing the string inside the [], not the path to it.

Collection.updateOne({
 _id: id
},
{
 $set: {
 "steps.$[steps].stepUsers.$[stepUsers].status": "PENDING",
 
 }
},
{
 arrayFilters: [
 {
 "steps._id": step._id
 },
 {
 "stepUsers._id": stepUser._id
 }
 ]
})

See how it works on the playground example

answered Feb 2, 2023 at 12:38
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it worked. Can you explain why i am changing to the arrayFilters: "steps.stepUsers._id" -> "stepUsers._id"
Updated the answer to explain, I hope it is clear...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.