具有多个参数的MongoDB pullAll对象

具有多个参数的MongoDB pullAll对象,mongodb,pull,mongodb-query,Mongodb,Pull,Mongodb Query,我只想使用pullAll从mongoDB中的数组中删除中的几个对象 db.collection.update({'_id': ObjectId(".....")}, { $pullAll : { 'notifications' : [{'type' : type}, {'id': id}]} }) 为什么这不起作用?正确的语法是什么 更新: 该文件是: { "_id" : ObjectId("......"), "notifications" : [ { "type" : "a

我只想使用pullAll从mongoDB中的数组中删除中的几个对象

db.collection.update({'_id': ObjectId(".....")}, { $pullAll : { 'notifications' : [{'type' : type}, {'id': id}]} })
为什么这不起作用?正确的语法是什么

更新:

该文件是:

{
    "_id" : ObjectId("......"),
    "notifications" : [ { "type" : "aaa",
                          "id" : "123" },
                        { "type" : "bbb",
                          "id" : "123" },
                        { "type" : "ccc",
                          "id" : "234" }]
}

要使用$pullAll,您需要精确匹配整个对象。为什么不直接使用$pull呢?我相信它会满足您的需要。

您的问题可能是以下两个方面之一:

首先,您的更新存在语法问题:

db.collection.update({'_id': ObjectId(".....")}, 
     { $pullAll : 
         { 'notifications' : [{'type' : type}, {'id': id}]
         } 
     }
)
应该是:

db.collection.update({'_id': ObjectId(".....")}, 
     { $pullAll : 
         { 'notifications' : [{'type' : type, 'id': id}]
         }  
     }
)
注意:我删除了
},{
并将type和id合并到一个JSON子文档中


另一个问题是数组元素似乎有
id
值,这些值是形式为“123”的字符串-您确定要将字符串传递给update语句吗?字符串“123”不等于整数123。

文档的结构是什么?看看我刚才用
db.collection.update({u id]:ObjectId)尝试的这个链接(“…”)}、{$pull:{'notifications':[{'type':type}、{'id':id}]},False,True)
db.collection.update({'id':ObjectId(“…”)}、{$pull:{'notifications':{'type':type,'id':id}},False,True)
我有相同的resultTry和$pull,只使用“type”或“id”,只是为了看看它是否有效