I am trying to update an object inside an array of a document using mgo. The object structure 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
- }
- }
- ]
- }]
- }
Query1:
I am first trying to update details > obj
where trying to add another attribute "guava": 15
using the following query
- cond := bson.M{ "$and": []bson.M{ bson.M{"document.details":bson.M{ "$elemMatch": bson.M{ "category": "A"} } }, bson.M{"document.status":0} } }
- query := bson.M{ "$set": bson.M{ "document.details.$.obj.guava":15 } }
- _, err := models.DbUpdateAll(Collection, cond, query)
This query is neither producing any error nor updating the doc. Can anyone please tell how can I accomplish it
Note: I have searched over google but could not find relevant to what I need.
Query2:
I also need to update the details > members > obj
the same way I am trying to do for details > obj
. Please also tell me how can I achieve the same for details > members > obj
.
I have spent hours on figuring this out but nothing worked out. I shall be thankful if anyone could guide me.