MongoDB:如何在MongoDB Shell中删除集合的所有记录? [英] MongoDB: How To Delete All Records Of A Collection in MongoDB Shell?

查看:434
本文介绍了MongoDB:如何在MongoDB Shell中删除集合的所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过

db.users.remove(*)

尽管它返回错误,但如何清除所有记录?

Although it returns an error so how do I go about clearing all records?

推荐答案

remove()的参数是一个过滤器文档,因此传递空文档意味着全部删除":

The argument to remove() is a filter document, so passing in an empty document means 'remove all':

db.user.remove({})

但是,如果您确实想删除所有可能,则最好删除该集合.虽然这可能取决于您是否在集合上具有用户定义的索引,即删除集合后准备集合的成本是否超过了remove()调用相对于drop()调用的较长持续时间.

However, if you definitely want to remove everything you might be 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.

更多详细信息在文档中.

这篇关于MongoDB:如何在MongoDB Shell中删除集合的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆