Using $ each with $ push - clicking the whole object in mongodb

My mongodb document

{ "_id" : { "coid" : "testcoid", "cid" : "testcid" }, "communications" : [ { "sid" : "testsid", "campid" : "testcampid" } ] } 

I want to finally add a click field and add some values

 { "_id" : { "coid" : "testcoid", "cid" : "testcid" }, "communications" : [ { "sid" : "testsid", "campid" : "testcampid", "clicks" : {"www.google.com" , "www.facebook.com"} } ] } 

I use the command

 db.messages.update({$and : [{"_id.coid" : "testcoid"}, {"communications.sid" : "testsid"}]},{ $push : {"communications.$.clicks" : {$each : ["www.google.com" , "www.facebook.com"]}}}) 

which gives this document instead

 db.messages.find().pretty() { "_id" : { "coid" : "testcoid", "cid" : "testcid" }, "communications" : [ { "campid" : "testcampid", "clicks" : [ { "$each" : [ "www.google1.com", "www.google2.com" ] } ], "sid" : "testsid" } ] } 

Note. I need to do this using push, not pushAll. Is there a way to do this with push and why does it update it with $ of each object?

+4
source share
1 answer

Your request worked on my field, http://pastebin.com/STUjCuMd

What version of mongodb are you using?

Check it out: https://jira.mongodb.org/browse/SERVER-8303

+2
source

Source: https://habr.com/ru/post/1488216/


All Articles