Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating sub object in mongodb

Tags:

mongodb

casbah

Consider following entry in my mongo collection.

{
  "id" : "_0001",
  "map" : {
            "foo" : 1
          }
}

Now i need to add an entry to inside map. like

{
  "id" : "_0001",
  "map" : {
            "foo" : 1,
            "bar" : 2,
          }
}

map is not a list so i cannot use $push there. Is there any way of doing this in mongo. And also I'm accessing the database via casbah. Please note down the casbah codes if you can.

NOTE:- map should not be a list

like image 446
tiran Avatar asked Oct 16 '25 00:10

tiran


1 Answers

Have you tried:

db.test.update({ _id : "_0001"}, { $set : { "map.bar" : 2}})
like image 177
kongo2002 Avatar answered Oct 17 '25 13:10

kongo2002



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!