ixx
ixx

Reputation: 32267

MongoDB multiple update in nested object doesn't work

I'm trying to run a multiple update of a field of an object in an array. The structure is like:

{
rs:[
  {uid:"123", ufc:"bla"},
  {uid:"123", ufc:"foo"}, 
  //...
  ]
}

For some reason only the first field is updated. I read I have to use multi:true to solve this, but it's still the same.

I tried:

db.mycollection.update({"rs.uid": "123"},
                  {$set: {"rs.$.ufc":"test"}},
                  false, true
                  )

And:

db.mycollection.update({"rs.uid": "123"},
                  {$set: {"rs.$.ufc":"test"}},
                  {multi: true}
                  )

Probably related with the nested structure? Thanks.

Upvotes: 1

Views: 490

Answers (1)

fgakk
fgakk

Reputation: 1307

I think you have hit the same problem as mentioned in this question

. As solution either wait for the following issue to be solved or update array elements one by one. In your query multi means updating multi document not item of any array field. The query works like this: Update all documents which has an item in rs field having uid "123".

Of course as an alternative if possible change structure of your schema supporting items of your rs array being a document themselves.

Hope this helps

Upvotes: 2

Related Questions