问题
I am currently using the $pull operator successfully to delete an item in an array. The Data structure looks like this
{
_id: 234,
comments: [{id:1},{id:2},{id:3}]
}
My change struct
change := mgo.Change{
Update: bson.M{
"$pull": bson.M{
"comments": bson.M{
"id": c.Id,
},
},
},
ReturnNew: true,
}
and my mongo command
test, err := cs.Find(bson.M{"_id": 234}).Apply(change, nil)
In the mongo shell, simulating the same command would return useful meta information
On success WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
on Fail WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
However in Go, the results of test
is &{Updated:1 Removed:0 Matched:1 UpsertedId:<nil>}
regardless of whether it successfully updated anything or not. Of course if it can't find the Document, it will return an error, but if it finds the document but fails in the $pull request, it doesn't give me any sort of indication.
My Question: How can I determine whether an array modifier was successful or not without having to query the database a second time
来源:https://stackoverflow.com/questions/35638441/mongodb-pull-success-indicator-with-mgo-golang