I have a requirement to update sub document of my mongoDB document which is nested as array of array as shown below in json structure:
{
"_id": "53cf4e3dae92ac6561807f6d",
"results": {
"trips": [
{
"_id": 1,
"name": "A",
"address": [
{
"_id": 123,
"value": {
"allowed": true,
"optional": false
}
},
{
"_id": 456,
"value": {
"allowed": false,
"optional": false
}
}
]
},
{
"_id": 2,
"name": "B",
"address": [
{
"_id": 789,
"value": {
"allowed": true,
"optional": false
}
},
{
"_id": 567,
"value": {
"allowed": false,
"optional": false
}
}
]
}
]
}
}
Now, I want to update results.trips.address._id =456 with new json element "value" object. I can think of using update query with $elemMatch but may be I am not using correctly so, it is not working for me. Also, I would be doing all this in java using spring data 1.4.x and mongoDB version is 2.6. Any help would be highly appreciated.
Query which I am using is:
db.collection.update({"_id" : ObjectId("53cf4e3dae92ac6561807f6d"), "results.trips.address":{$elemMatch : {"results.trips.address._id":456}}},{$set : {"results.trips.address.value":{"allowed": true, "optional": true}}});