doupoji3856 2017-01-24 16:16 采纳率: 100%
浏览 330

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

I have this on my mongo database :

{
"_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")
    }, ...

I want to remove on element from this array. I'm using golang and mgo.v2 driver and here is my code :

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")
    }

It goes on Success but does not delete the item when I check on mongodb. Can soemeone tell me what I'm doing wrong ? Thank you

  • 写回答

1条回答 默认 最新

  • dpb56083 2018-01-04 07:42
    关注

    maybe you need to specify the field name in apps object just like

    update := bson.M{"$pull": bson.M{"apps": bson.M{"_id":bson.ObjectIdHex("585a61405ba19c3eb2016a17")}}}

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能