0

I have mongodb with the below collection

management_model:PRIMARY> db.info.find()
{
        "_id" : ObjectId("58c78f1fbda0608671cd06c6"),
        "name" : "somename",
        "age" : 25
}
{
        "_id" : ObjectId("58c791b9219b7932ab093f20"),
        "name" : "Alice",
        "age" : 26
}
{
        "_id" : ObjectId("58c79351a04681dda3314d6f"),
        "name" : "Bob",
        "age" : 36,
        "additional" : [
                {
                        "old" : "No"
                }
        ]
}

I want to update the "old" : "yes" using update() method but when i try the following ways, nothing seems to work.

1.

management_model:PRIMARY> db.info.update( {_id: "58c79351a04681dda3314d6f"}, {$set: {"additional.0.old": "yes"} } )
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })


management_model:PRIMARY> db.info.update( {_id: "58c79351a04681dda3314d6f"}, {$set: {"additional.old": "yes"} } )
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

All of the above queries return saying no match found. Am i missing something ?

-Prashanth

pkumarn
  • 1,383
  • 4
  • 22
  • 29
  • Is your additional array has _id too? – Vaibhav Patil Mar 14 '17 at 10:02
  • Using the concept in the marked dupe, your update operation should be `db.info.update( {_id: "58c79351a04681dda3314d6f", "additiona.old": "No" }, {$set: {"additional.$.old": "yes"} } )` – chridam Mar 14 '17 at 10:16
  • Thanks for the reply. Even with the highlighted query it still fails. I don't have _id filed in array. – pkumarn Mar 15 '17 at 05:55

0 Answers0