Search code examples
mongodbcasbah

updating sub object in mongodb


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


Solution

  • Have you tried:

    db.test.update({ _id : "_0001"}, { $set : { "map.bar" : 2}})