从嵌套数组golang mongodb中删除元素

问题描述 投票:0回答:1

我在我的mongo数据库上有这个:

{
"_id" : ObjectId("585a60b45ba19c3eb2016a16"),
"userid" : ObjectId("585a60715ba19c3eb2016a14"),
"apps" : [
    {
        "name" : "Apptest",
        "packagename" : "com.test.ezfez",
        "iconurl" : "an_url",
        "visited" : 0,
        "category" : "category",
        "_id" : ObjectId("585a61405ba19c3eb2016a17")
    },
    {
        "name" : "test",
        "packagename" : "test.packagename",
        "iconurl" : "url",
        "visited" : 0,
        "category" : "",
        "_id" : ObjectId("588755025ba19c6f870282d7")
    }, ...

我想从这个数组中删除元素。我正在使用golang和mgo.v2驱动程序,这是我的代码:

selector := bson.M{"userid": bson.ObjectIdHex("585a60715ba19c3eb2016a14")}
    update := bson.M{"$pull": bson.M{"apps": bson.ObjectIdHex("585a61405ba19c3eb2016a17")}}
    if err := uc.session.DB("API").C("aioapps").Update(selector, update); err != nil {
        fmt.Println(err)
        SendError(w, "Remove", "Error on delete app")
    } else {
        SendSuccess(w, "Remove", "Success")
    }

它继续成功但在我检查mongodb时不删除该项目。有人能告诉我我做错了什么吗?谢谢

mongodb go
1个回答
0
投票

也许您需要在apps对象中指定字段名称

更新:= bson.M {“$ pull”:bson.M {“apps”:bson.M {“_ id”:bson.ObjectIdHex(“585a61405ba19c3eb2016a17”)}}}

© www.soinside.com 2019 - 2024. All rights reserved.