使用 mongodb 的 java 中的 createIndex() 和 ensureIndex() 之间的区别
在Java中使用MongoDB有什么区别?我用谷歌搜索了这个,但没有得到满意的答案。createIndex()
ensureIndex()
在Java中使用MongoDB有什么区别?我用谷歌搜索了这个,但没有得到满意的答案。createIndex()
ensureIndex()
更新 2:原始答案以及第一次更新错误地引用了Mongo shell文档而不是Java API。
在 Java 中,DBCollection.ensureIndex()
在 2.12 版中被弃用,在 3.0 版中被移除。DBCollection.createIndex()
是应该使用的那个。
更新:
自版本 3.0.0 起已弃用。
现在是 db.collection.createIndex() 的别名。db.collection.ensureIndex()
原始版本:
自 1.8 起已弃用createIndex()
它用于在集合上创建索引,而在指定字段上创建索引(如果索引尚不存在)。此外,当我们执行两次时,第二次执行将失败,而您可以多次调用它并且它不会失败ensureIndex()
createIndex()
ensureIndex()
在以前版本的mongodb(版本小于2.6)中,如果现有文档的索引条目超过最大索引键长度,则会创建索引,但Mongodb不会为此类文档编制索引,而在最新版本中则不会创建索引。ensureIndex()
在Java API中,被弃用,与“正常”MongoDB API(在响应时)相比,恰恰相反。更新:这种不一致似乎已经解决,现在Mongo shell中也替代了。DBCollection.ensureIndex()
db.collection.createIndex()
db.collection.ensureIndex()
正如您在 https://jira.mongodb.org/browse/JAVA-1097 中看到的,在Java中(OP询问了该操作)在Java驱动程序的2.12.0版本中已被弃用,DBCollection.createIndex()
是您需要使用的那个。DBCollection.ensureIndex()
(指向版本 2.12 的链接) 在 DBCollection Java API 中不再可用。ensureIndex()