MongoDB:如何在 MongoDB Shell 中删除集合的所有记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46368368/
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
MongoDB: How To Delete All Records Of A Collection in MongoDB Shell?
提问by glytching
I've tried
我试过了
db.users.remove(*)
Although it returns an error so how do I go about clearing all records?
虽然它返回一个错误,那么我该如何清除所有记录?
回答by glytching
The argument to remove()
is a filter document, so passing in an empty document means 'remove all':
to 的参数remove()
是一个过滤器文档,因此传入一个空文档意味着“全部删除”:
db.user.remove({})
However, if you definitely want to remove everything you mightbe better off dropping the collection. Though that probably depends on whether you have user defined indexes on the collection i.e. whether the cost of preparing the collection after dropping it outweighs the longer duration of the remove()
call vs the drop()
call.
但是,如果你一定要删除所有你可能被关闭丢弃收集更好。虽然这可能取决于您是否在集合上有用户定义的索引,即在删除集合后准备集合的成本是否超过了remove()
通话与drop()
通话的较长持续时间。
More details in the docs.
文档中的更多详细信息。
回答by N00b Pr0grammer
You can delete all the documents from a collection in MongoDB, you can use the following:
您可以从 MongoDB 的集合中删除所有文档,您可以使用以下命令:
db.users.remove({})
Alternatively, you could use the following method as well:
或者,您也可以使用以下方法:
db.users.deleteMany({})
Follow the following MongoDB documentation, for further details.
请按照以下 MongoDB文档了解更多详细信息。
To remove all documents from a collection, pass an empty filter document
{}
to either thedb.collection.deleteMany()
or thedb.collection.remove()
method.
要从集合中删除所有文档,请将空过滤器文档传递
{}
给db.collection.deleteMany()
或db.collection.remove()
方法。
回答by Omkesh Sajjanwar
db.users.count()
db.users.remove({})
db.users.count()