I am trying to update the following document in Mongodb.
doc = { id : 10 , graph :[{userId:1,children:[2]},{userId:2,children:[]}]}
db.test.insert(doc)
then I perform two updates:
db.test.update( {'id':10,'graph.userId' : 1}, { $push:{'graph.$.children':10}})
db.test.update( {'id':10,'graph.userId' : 1},{ $push:{'graph':{'userId':10,'children':[]}}})
(Saddly :
db.test.update( {'id':10,'graph.userId' : 1},{ $push:{'graph.$.children':10},$push:{'graph':{'userId':10,'children':[]}}})
does not work)
Is there a way to update these simultaneously ?
Thanks a lot
The multiple keys in the modifier array stop it from working. You have to do two updates to do this if I read your shema right cos you are trying to push a new child onto the current position and push a new record into the subdocument of the parent.
The thing that stops it is the children[] setting. Mongo just does not know where to set that.
I suppose you could try:
But it is a long shot