mongodb 在单个查询中从 mongo 中删除多个文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18566590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:24:01  来源:igfitidea点击:

Remove multiple documents from mongo in a single query

mongodbpymongo

提问by Anurag Sharma

I have a list of mongo '_id' which I want to delete. Currently I am doing this

我有一个要删除的 mongo '_id' 列表。目前我正在这样做

# inactive_users -->  list of inactive users 
for item in inactive_users:
    db.users.remove({'_id' : item})

but my problem is the list is too huge... (it might go 100,000 +). So querying for every item in list will only increase the load on server. Is their a way to pass the entire list in mongo query so that I dont have to fire query again and again.

但我的问题是列表太大了......(可能会超过 100,000)。所以查询列表中的每一项只会增加服务器的负载。是他们在 mongo 查询中传递整个列表的一种方式,这样我就不必一次又一次地触发查询。

Thank you

谢谢

回答by Roman Pekar

db.users.remove({'_id':{'$in':inactive_users}})

回答by Yevgeniy Anfilofyev

List them all and use $inoperator:

将它们全部列出并使用$in运算符:

db.users.remove({_id:{$in:[id1, id2, id3, ... ]}})

回答by julien bouteloup

You need to pass the ids in a specific format using ObjectId():

您需要使用ObjectId()以下特定格式传递 ID :

db.users.remove({_id: {$in: [ObjectId('Item1'), ObjectId('Item2'), ObjectId('Item2')]}});

Removedoesn't accept integer - you have to use ObjectIdinstance with _idformat as a string.

Remove不接受整数-你必须使用ObjectId实例与_id格式作为string

回答by VIctor Hugo

var collection = db.users;
var usersDelete = [];
var ObjectID = req.mongo.ObjectID;   //req is request from express

req.body.forEach(function(item){     //req.body => [{'_id' : ".." , "name" : "john"}]
    usersDelete.push(new ObjectID(item._id));
});

collection.remove({'_id':{'$in': usersDelete}},function(){
    //res.json(contatos);
});

回答by docb45

I had the same question and ran across these answers but it seems the MongoDB manual is recommending deleteMany instead of remove. deleteMany returns the delete count as well as an acknowledgement of the write concern (if the operation succeeded).

我有同样的问题并遇到了这些答案,但似乎 MongoDB 手册建议使用 deleteMany 而不是 remove。deleteMany 返回删除计数以及写入问题的确认(如果操作成功)。

const ids = [id1, id2, id3...];
const query = { _id: { $in: ids} };
dbo.collection("users").deleteMany(query, function (err, obj) {
    if (err) throw err;
});

Or with an arrow function:

或者使用箭头函数:

const ids = [id1, id2, id3...];
const query = { _id: { $in: ids} };
dbo.collection("users").deleteMany(query, (err, obj) => {
    if (err) throw err;
});

Or better yet, with a promise:

或者更好的是,承诺:

const ids = [id1, id2, id3...];
const query = { _id: { $in: ids} };
dbo.collection("users").deleteMany(query)
.then(result => {
    console.log("Records Deleted");
    console.log(JSON.stringify(result));
    //for number removed...
    console.log("Removed: " + result["n"]);
})
.catch(err => {
    console.log("Error");
    console.log(err);
});