MongoDB $pull success indicator with mgo Golang

三世轮回 提交于 2021-02-10 12:49:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!