如何删除mongodb中的多个ID?

时间:2013-07-24 05:44:53

标签: mongodb xamarin.ios mono mongodb-.net-driver

{ "_id" : ObjectId("51ee3966e4b056fe8f074f48"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}
{ "_id" : ObjectId("51ee507ae4b056fe8f074f4a"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}
{ "_id" : ObjectId("51ee51fee4b056fe8f074f4b"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}

如何在mongodb中删除多个ID?

2 个答案:

答案 0 :(得分:26)

为每个ID运行一次删除,或者使用in:

db.collection.remove( { _id : { $in: [
    ObjectId("51ee3966e4b056fe8f074f48"), 
    ObjectId("51ee3966e4b056fe8f074f4a"), 
    ObjectId("51ee3966e4b056fe8f074f4b") 
] } } );

答案 1 :(得分:0)

您可以使用命令deleteMany完成此操作。

const objects = [
    ObjectId("51ee3966e4b056fe8f074f48"), 
    ObjectId("51ee3966e4b056fe8f074f4a"), 
    ObjectId("51ee3966e4b056fe8f074f4b") 
];

db.collection.deleteMany({_id: { $in: objects}});