Meteor - Mongodb:从二级对象中删除值

时间:2016-12-24 02:24:09

标签: javascript mongodb meteor

我在一个集合中有一个文档,它是两个数组级别的深度。我似乎无法从Mongodb数据库中的Meteor项目中删除它们。基本上我想单独删除或“取消设置”“map_group.users”。当我单击按钮调用下面的方法时,它会删除所有这些。不是我特别想要的那个。

DATABASE enter image description here 客户js

'click .ig_map_remove_user'(event){         event.preventDefault()

    Meteor.call('remove_instragram_group_map_user', FlowRouter.current().params.group_name, this.username, this.id, this.profile_picture, function(err) {
        if (err) {
            console.log(err)
            console.error('Could not remove user from group.')
        } else {
            console.log('User removed from group')
        }
    })
}

服务器js

remove_instragram_group_map_user: function(group, username, id, profile_photo){
        instagram_collection.update({
            user_id: Meteor.userId(),
            "map_group.group_name": group
        }, {
            $unset: {
                "map_group.$.users": {
                    "username": username,
                    "id": id,
                    "profile_photo": profile_photo
                }
            }
        })
    }

2 个答案:

答案 0 :(得分:2)

您希望使用$pull而非$unset基本上删除该字段。假设用户名在该数组中是唯一的,您可以直接匹配:

    instagram_collection.update({
      user_id: Meteor.userId(),
      "map_group.group_name": group
    },{
      $pull: {
        "mapGroup.$.users": {
          "username": username
        }
      }
    })

答案 1 :(得分:1)

我是在Michels的帮助下得到的

instagram_collection.update({
            user_id: Meteor.userId(),
            "map_group.group_name": group
        }, {
            $pull: {
                "map_group.$.users": {
                    "username": username
                }
            }
        })
相关问题