简体   繁体   English

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

[英]Difference between createIndex() and ensureIndex() in java using mongodb

What is the difference between createIndex() and ensureIndex() in Java using MongoDB? 使用MongoDB在Java中createIndex()ensureIndex()什么区别? I googled this but didn't get a satisfactory answer. 我用Google搜索了这个,但没有得到满意的答案。

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. 在Java中, DBCollection.ensureIndex()在版本2.12中已弃用,在版本3.0中已删除。 DBCollection.createIndex() is the one that should be used. DBCollection.createIndex()是应该使用的。

Update: 更新:
db.collection.ensureIndex() is deprecated since version 3.0.0. 从版本3.0.0开始,不推荐使用db.collection.ensureIndex()
Is now an alias for db.collection.createIndex() . 现在是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 field if the index does not already exist. 它用于在集合上创建索引,而如果索引尚不存在, ensureIndex()会在指定字段上创建索引。 Moreover when we execute createIndex() twice the second execution will just fail whereas with ensureIndex() you can invoke it multiple times and it will not fail 此外,当我们执行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 created but Mongodb would not index such documents whereas in recent version no index would be created . 还有一件事是他们改变了ensureIndex()行为,在以前版本的mongodb(版本少于2.6)中,如果现有文档的索引条目超过最大索引键长度, 则会创建一个索引但是Mongodb 不会索引这样的文档,而在最近的版本中, 不会创建索引

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) . 在Java API中,不推荐使用DBCollection.ensureIndex() ,与“普通”MongoDB API(响应时)相比,完全相反 Update : This inconsistency appears to have since been resolved, and db.collection.createIndex() now substitutes db.collection.ensureIndex() in the Mongo shell also. 更新 :此不一致似乎已解决, db.collection.createIndex()现在也替换Mongo shell中的db.collection.ensureIndex()

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. 正如你可以看到在https://jira.mongodb.org/browse/JAVA-1097 ,在Java(其中OP询问) ensureIndex()已被废弃的Java驱动程序的版本2.12.0和DBCollection.createIndex()是你需要使用的那个。 DBCollection.ensureIndex() (link to version 2.12) is not available in the DBCollection Java API anymore. DBCollection.ensureIndex() (链接到版本2.12)在DBCollection Java API中不再可用。

The ensureIndex method found in the java driver (v2.12 and older) would cache whether or not the index exist on the collection. 在java驱动程序(v2.12和更早版本)中找到的ensureIndex方法将缓存集合中是否存在索引。 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. 由于多个客户端可能会更改集合上的索引,因此缓存值有时可能是错误的,并且驱动程序将无法创建缺失的索引。

For this reason, the java driver implemented a createIndex method with identical behaviour, except it won't cache the index status. 出于这个原因,java驱动程序实现了一个具有相同行为的createIndex方法,除了它不会缓存索引状态。

With drivers 2.12 and above, you can replace ensureIndex by createIndex and 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及更高版本,您可以通过createIndex替换ensureIndex并期望相同的行为,除了驱动程序以前认为索引已经存在的性能命中并返回而不将createIndex命令发送到mongo服务器。

As to why they did not change the behaviour without renaming - that I have no idea. 至于为什么他们没有重命名就不改变行为 - 我不知道。

自版本> 3.0.0后不推荐使用:db.collection.ensureIndex()现在是db.collection.createIndex()的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM