java中使用mongodb的createIndex()和ensureIndex()的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25968592/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:36:41  来源:igfitidea点击:

Difference between createIndex() and ensureIndex() in java using mongodb

javamongodbmongodb-java

提问by Amit Chahar

What is the difference between createIndex()and ensureIndex()in Java using MongoDB? I googled this but didn't get a satisfactory answer.

在 Java 中使用 MongoDBcreateIndex()ensureIndex()在 Java 中有什么区别?我用谷歌搜索了这个,但没有得到满意的答案。

采纳答案by sol4me

Update 2:The original answer, as well as the first update, erroneously reference the Mongo shell documentation instead of the Java API.

更新 2:原始答案以及第一次更新错误地引用了 Mongo shell 文档而不是 Java API。

In Java, DBCollection.ensureIndex()was deprecated in version 2.12 and removed in version 3.0. DBCollection.createIndex()is the one that should be used.

在 Java 中,DBCollection.ensureIndex()在 2.12 版中已弃用,并在 3.0 版中删除。DBCollection.createIndex()是应该使用的。

Update:
db.collection.ensureIndex()is deprecated since version 3.0.0.
Is now an alias for db.collection.createIndex().

更新:
db.collection.ensureIndex()自 3.0.0 版起已弃用。
现在是db.collection.createIndex()的别名。

Original:
createIndex()is deprecated since 1.8

原文:自 1.8 起
createIndex()弃用

It was used to create indexes on collections whereas ensureIndex()creates an index on the specified fieldif the index does not already exist. Moreover when we execute createIndex()twice the second execution will just failwhereas with ensureIndex()you can invoke it multiple times and it will not fail

它用于在集合上创建索引,而如果索引不存在,则在ensureIndex()指定的字段上创建索引。此外,当我们执行createIndex()两次时,第二次执行只会失败,ensureIndex()您可以多次调用它并且不会失败

And one more thing that they changed regarding behavior of ensureIndex(), in previous versions of mongodb(versions less then 2.6) if the index entry for an existing document exceeds the max index key length an index would be createdbut Mongodb would not indexsuch documents whereas in recent version no index would be created.

还有一件事,他们ensureIndex()在 mongodb 的早期版本(版本小于 2.6)中更改了关于行为的另一件事,如果现有文档的索引条目超过最大索引键长度,则将创建索引,但 Mongodb不会索引此类文档,而在最新版本不会创建索引

回答by Jens Grivolla

In the Java API, DBCollection.ensureIndex()is deprecated, exactly the other way around compared to the "normal" MongoDB API (at the time of the response). Update: This inconsistency appears to have since been resolved, and db.collection.createIndex()now substitutes db.collection.ensureIndex()in the Mongo shell also.

在 Java API 中,DBCollection.ensureIndex()已弃用,与“正常”MongoDB API(在响应时)相比,正好相反更新:此不一致似乎已得到解决,db.collection.createIndex()现在db.collection.ensureIndex()也在 Mongo shell 中替代。

As you can see in https://jira.mongodb.org/browse/JAVA-1097, in Java (which the OP asked about) ensureIndex()was deprecated in version 2.12.0 of the Java driver, and DBCollection.createIndex()is the one you need to use. DBCollection.ensureIndex()(link to version 2.12) is not available in the DBCollectionJava API anymore.

正如您在https://jira.mongodb.org/browse/JAVA-1097中看到的,在 Java 中(OP 询问的)ensureIndex()在 Java 驱动程序的 2.12.0 版中已被弃用,并且DBCollection.createIndex()是您需要使用的. DBCollection.ensureIndex()(链接到版本 2.12)在DBCollectionJava API 中不再可用。

回答by GaspardP

The ensureIndexmethod found in the java driver (v2.12 and older) would cache whether or not the index exist on the collection. Since multiple clients could potentially change the indexes on a collection, the cache value may sometime be erroneous and the driver would fail to create a missing index.

ensureIndex在 java 驱动程序(v2.12 及更早版本)中找到的方法将缓存集合中是否存在索引。由于多个客户端可能会更改集合上的索引,因此缓存值有时可能是错误的,驱动程序将无法创建丢失的索引。

For this reason, the java driver implemented a createIndexmethod with identical behaviour, except it won't cache the index status.

出于这个原因,java 驱动程序实现了一个createIndex具有相同行为的方法,只是它不会缓存索引状态。

With drivers 2.12 and above, you can replace ensureIndexby createIndexand expect the same behaviour, except for the performance hit where the driver would formerly think that the index already exists and return without sending the createIndex command to the mongo server.

使用驱动程序 2.12 及更高版本,您可以替换并期望相同的行为,除了性能ensureIndex下降createIndex,驱动程序以前认为索引已经存在并返回而不向 mongo 服务器发送 createIndex 命令。

As to why they did not change the behaviour without renaming - that I have no idea.

至于为什么他们没有重命名就没有改变行为 - 我不知道。

回答by Surender Singh

Deprecated since version > 3.0.0: db.collection.ensureIndex() is now an alias for db.collection.createIndex().

版本 > 3.0.0 后已弃用:db.collection.ensureIndex() 现在是 db.collection.createIndex() 的别名。