在MongoDB中,如果删除集合,索引也会自动下降? [英] In MongoDB, if collection is dropped, indexes dropped automatically as well?

查看:968
本文介绍了在MongoDB中,如果删除集合,索引也会自动下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Mongo中创建一个集合,并且在向该集合添加文档之后,我使用 ensureIndex()在文档上的数字字段上创建一个索引

If I create a collection in Mongo, and after adding documents to this collection I use ensureIndex() to create an index on, say, a number field on a document in this collections, if I drop the collection, do I have to recreate the index?

推荐答案

简短的答案:是的。

索引在收集中下降。您需要重新创建一个索引。

Indexes are dropping on collection drop. You need to recreate an index.

您可能不希望删除集合,但删除其中的所有项目 db.collection_name.remove({ })。它将需要更多的资源,但留下您的索引。实际上,它将需要删除所有索引数据。这就是为什么更倾向于删除整个集合,然后重建索引。

You may want to not to drop collection but remove all items in it with db.collection_name.remove({}). It will take more resources but leave your indexes. Actually it will need to delete all index data. That is why it is more preferred to drop the whole collection and recreate indexes after that.

这篇关于在MongoDB中,如果删除集合,索引也会自动下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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