简体   繁体   English

mongodb 中的 deleteMany() 不删除文档

[英]deleteMany() in mongodb not deleting documents

i have the following records in the collection ilists我在收藏列表中有以下记录

db.ilists.find() db.ilists.find()

{ "_id" : ObjectId("5efb10f25372d01d549954b1"), "name" : "hello user", "__v" : 0 }
{ "_id" : ObjectId("5efb10f25372d01d549954b2"), "name" : "click + to add items to the list", "__v" : 0 }
{ "_id" : ObjectId("5efb10f25372d01d549954b3"), "name" : "<---click the checkbox to deletet item in the list", "__v" : 0 }
{ "_id" : ObjectId("5efb170a32b1290c64950999"), "__v" : 0 }
{ "_id" : ObjectId("5efb17790d9ff700204c43c2"), "__v" : 0 }

i have two empty documents and i am using deleteMany to delete them but i am getting the following我有两个空文档,我正在使用 deleteMany 删除它们,但我得到以下

> db.ilists.deleteMany({"_id":"5efb170a32b1290c64950999"},{"_id":"5efb17790d9ff700204c43c2"})
{ "acknowledged" : true, "deletedCount" : 0 }

as you can see the deleted count is 0 and those documents are not being deleted any help for this?如您所见,已删除计数为 0,并且这些文档没有被删除,对此有何帮助?

According to the documentation , deleteMany expects filter query as the first argument.根据文档deleteMany期望过滤器查询作为第一个参数。 In your case, since the first argument does seem like a query, it's not working as expected.在您的情况下,由于第一个参数确实看起来像一个查询,因此它没有按预期工作。

You may try using the following:您可以尝试使用以下方法:

db.ilists.remove( { _id : { $in: [
    ObjectId("5efb170a32b1290c64950999"), 
    ObjectId("5efb17790d9ff700204c43c2"), 
] } } );

Alternatively, if you plan to delete all the empty documents, you can update your deleteMany query as below:或者,如果您打算删除所有空文档,您可以更新您的deleteMany查询,如下所示:

db.ilists.deleteMany({ name: {$exists: false} })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM