I recently started using MongoDB, and I have a question regarding updating arrays in a document. I got a structure like this:
{ "_id" : ObjectId(), "post" : "", "comments" : [ { "user" : "test", "avatar" : "/static/avatars/asd.jpg", "text" : "....." } { "user" : "test", "avatar" : "/static/avatars/asd.jpg", "text" : "....." } { "user" : "test", "avatar" : "/static/avatars/asd.jpg", "text" : "....." } ... ] }
I am trying to execute the following query:
update({"comments.user":"test"},{$set:{"comments.$.avatar": "new_avatar.jpg"}},false,true)
The problem is that it updates all documents, but only the first element of the array in each document updates. Is there a way to update all elements of an array, or should I try to do this manually? Thanks.
arrays mongodb
Viktor Kirilov
source share