I have searched over the web for this issue, the conclusion where I have reached is that it is a bug in the latest version of mongo as told here and here
The problem is that the structure of the object is as following:
{
"_id": 2,
"status": 0,
"details": [{
"id": 2,
"category": "A",
"obj": {
"apple": 5,
"banana": 2,
"cherry": 10
},
"members": [{
"id": 3,
"category": "A",
"obj": {
"apple": 5,
"banana": 2,
"cherry": 10
}
},
{
"id": 4,
"category": "A",
"obj": {
"apple": 5,
"banana": 2,
"cherry": 10
}
}
]
}]
}
which I am using everywhere in my project which is almost ready. But now the requirement is to update the obj
inside the members
object. For which I tried
cond := bson.M{ "$and": []bson.M{ bson.M{"details":bson.M{ "$elemMatch": bson.M{ "category": "A"} } }, bson.M{"status":0} } }
query := bson.M{ "$set": bson.M{ "details.$.members.$.obj.guava":15 } }
_, err := models.DbUpdateAll(Collection, cond, query)
But it is not working. Do I need to change the whole structure of the document by maintaining the array in different collection and passing reference to this object? It will be really a bad idea as for now where the project stands.... Isn't there any easy solution?