node.js Mongoose - 在一个函数调用中删除多个文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44467318/
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
Mongoose - remove multiple documents in one function call
提问by Maciej Krawczyk
In documentation there's deleteMany() method
在文档中有 deleteMany() 方法
Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {});
I want to remove multiple documents that have one common property and the other property vary. Something like this:
我想删除具有一个共同属性而另一个属性不同的多个文档。像这样的东西:
Site.deleteMany({ userUID: uid, id: [10, 2, 3, 5]}, function(err){}
Site.deleteMany({ userUID: uid, id: [10, 2, 3, 5]}, function(err){}
What would be the proper syntax for this?
什么是正确的语法?
回答by Kevin
I believe what youre looking for is the $inoperator:
我相信你要找的是$in运营商:
Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err) {})
Documentation here: https://docs.mongodb.com/manual/reference/operator/query/in/
此处的文档:https: //docs.mongodb.com/manual/reference/operator/query/in/
回答by laxman
You can also use.
您也可以使用。
Site.remove({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err, response) {});
回答by estani
I had to change idto _idfor it to work:
我不得不改变id,以_id它的工作:
Site.deleteMany({ _id: [1, 2, 3] });
This happens if no id is defined and the default one is used instead:
如果未定义 id 而使用默认值,则会发生这种情况:
"Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema constructor." mongoose docs
“如果没有将 _id 字段传递给 Schema 构造函数,Mongoose 会默认为每个模式分配一个 _id 字段。” 猫鼬文档
回答by ramana vv
Yes, $inis a perfect solution :
是的,$in是一个完美的解决方案:
Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5] } }, function(err) {})
Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5] } }, function(err) {})

