0

I have the following document and want to update state

Document ID: ObjectId("5a4e5a448b70d50e34d204a5")

Target ID: ObjectId("5a4e5a438b70d50e34d203ea")

I have no idea how to update the state to e.g. 4

  {
  "_id" : ObjectId("5a4e5a448b70d50e34d204a5"),
  "name" : "Wirtschaftsdienst",
  "date" : ISODate("2012-10-07T00:00:00.000Z"),
  "comment" : null,
  "tasks" : [ 
      {
          "name" : "Speisen und Getränke",
          "sections" : [ 
              {
                  "start" : 46800,
                  "end" : 72000,
                  "entirely" : true,
                  "assistants" : [ 
                      {
                          "assistant" : {
                              "_id" : ObjectId("5a4e5a438b70d50e34d203ea")
                          },
                          "state" : 3
                      }, 
                      {
                          "assistant" : {
                              "_id" : ObjectId("5a4e5a438b70d50e34d203f4")
                          },
                          "state" : 3
                      }
                  ]
              }
          ]
      }
  ]
 }

1 Answer 1

1

Use positional operator $[] along with arrayFilters to get your job done!

Try this query:

db.collection.update(
 {"_id" : ObjectId("5a4e5a448b70d50e34d204a5")},
 {$set: {"tasks.$[].sections.$[].assistants.$[element].state":4}},
 {arrayFilters: [ {"element.assistant":{"_id" : 
  ObjectId("5a4e5a438b70d50e34d203ea")} } 
  ], multi:true}
)

And the output is:

/* 1 */
{
"_id" : ObjectId("5a4e5a448b70d50e34d204a5"),
"name" : "Wirtschaftsdienst",
"date" : ISODate("2012-10-07T00:00:00.000Z"),
"comment" : null,
"tasks" : [ 
    {
        "name" : "Speisen und Getränke",
        "sections" : [ 
            {
                "start" : 46800,
                "end" : 72000,
                "entirely" : true,
                "assistants" : [ 
                    {
                        "assistant" : {
                            "_id" : ObjectId("5a4e5a438b70d50e34d203ea")
                        },
                        "state" : 4.0
                    }, 
                    {
                        "assistant" : {
                            "_id" : ObjectId("5a4e5a438b70d50e34d203f4")
                        },
                        "state" : 3.0
                    }
                ]
            }
        ]
    }
 ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. That looks promising but I get the error cannot use the part (tasks of tasks.$[].sections.$[].assistants.$[element].state) to traverse the element ({tasks: [ { name: "Speisen und Getränke", sections: [ { start: 46800, end: 72000, entirely: true, assistants: [ { assistant: { _id: ObjectId('5a4e5a438b70d50e34d203ea') }, state: 3 }, { assistant: { _id: ObjectId('5a4e5a438b70d50e34d203f4') }, state: 3 } ] } ] } ]})
Are u executing the query from robomongo? U need to execite it from mongo shell and preferrably mongo 3. 6. Even I was getting error when I tried from third party tool such as robo, once I tried same query in mongo shell, it worked.
Thank you so much! I had mongodb 3.6 already but my nodejs mongoclient was prior 3.x and did not support arrayFilters. Now everything is working great ;-)

Your Answer

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.