Thursday, 15 January 2015

How to Update Multiple Array Elements in mongodb -


Text after "

I have a Mongo document that holds an array of elements.

I want to reset the .handled attribute of all the objects in the array where .profile = XX. Document is as follows:

  { "_ id": ObjectId ( "4d2d8deff4e6c1d71fc29a07"), "user_id": "714638ba-2e08-2168-2b99-00002f3d43c0", "events": [ {"Handled": 1, "Profile": 10, "Data": "....."} {"Handled": 1, "Profile": 10, "Data": "....."} { "Handled": 1, "Profile": 20, "data": "....."} ...]}  

so, I tried the following: < / p>

  .update ({ "events.profile": 10}, {$ set: { "events held $ ..": 0}}, false, true)  

Although it only updates the element first matching element in each document only ( This is the reason that is defined behavior.)

How can I not all match array elements?

Using Sthitiik operator at this time is not possible to update all the items in an array. See JIRA

can as you work around that:

    Update
  • In each item individually (events.1.handled events.0. handled ...) or ...
  • Read the document, manually edit and save it to replace the old one (see if you want to make sure nuclear update)

No comments:

Post a Comment