【问题标题】:Does MongoDB ensureIndex perform a rebuild?MongoDB ensureIndex 是否执行重建?
【发布时间】:2014-04-17 02:37:29
【问题描述】:

我正在阅读 MongoFB 文档。 在这个网址 http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/ 我读 "使用 mongo shell 中的 ensureIndex() 或驱动程序中的类似方法创建新索引。 此操作将在此 mongod 实例上创建或重建索引"

我明白了吗? 如果索引已经存在,mongoDB 是否会执行重建?

因此,与“reIndex()”的区别在于 reIndex() 对集合的所有索引执行重建。 对吗?

【问题讨论】:

  • 我认为这可能是一个英文错误,如果索引已经存在,或者至少应该存在,则 ensureIndex() 注册一个空操作

标签: mongodb mongodb-indexes


【解决方案1】:

我认为这要么是一个英语错误,要么是在谈论您是否要在副本集上重建索引,ensureIndex() 的文档特别指出:

http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/#behaviors(从这里链接到的那个:http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/#build-the-index

如果同时调用多个具有相同索引规范的 ensureIndex() 方法,只有第一个操作会成功,其他所有操作无效。

因此,除非您正在重建索引,否则再次调用它不会导致重建。

如果要重建索引,必须先将其删除,然后重新运行ensureIndex()

要添加或更改索引选项,您必须使用 dropIndex() 方法删除索引并使用新选项发出另一个 ensureIndex() 操作。

如果您使用一组选项创建索引,然后使用相同的索引字段和不同的选项发出 ensureIndex() 方法而不首先删除索引,则 ensureIndex() 不会使用新选项重建现有索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2011-11-16
    • 2011-10-23
    • 2013-10-31
    • 2013-05-03
    • 2012-10-27
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多