createIndex vs ensureIndex

986 views
Skip to first unread message

Swifty D

unread,
Dec 16, 2014, 6:09:12 PM12/16/14
to mongod...@googlegroups.com

Is there a difference between createIndex and ensureIndex?

I note that in the shell documentation createIndex is deprecated and in the C# and Java drivers ensureIndex is deprecated.

Stephen Steneker

unread,
Dec 16, 2014, 7:34:10 PM12/16/14
to mongod...@googlegroups.com
On Wednesday, 17 December 2014 10:09:12 UTC+11, Swifty D wrote:

Is there a difference between createIndex and ensureIndex?

I believe the original intent of the methods was that "createIndex" returns a status if the index already exists, while "ensureIndex" does not. There is no "ensureIndex" command on the server side, so the mongo shell and drivers are actually providing the "ensure" logic.
 
I note that in the shell documentation createIndex is deprecated and in the C# and Java drivers ensureIndex is deprecated.

This is indeed confusing -- I've raised issues to make the shell documentation consistent with the drivers: DOCS-4539, DOCS-4540.

FYI, some of the drivers had ensureIndex logic to cache whether an index had already been created (in an attempt to save round trips to the server) but the implementations of createIndex & ensureIndex were otherwise identical. The convention of "createIndex" was chosen to align with the server command "createIndexes" which was added in MongoDB 2.6. For some background you can see discussion on: https://jira.mongodb.org/browse/JAVA-1097.

Regards,
Stephen
Reply all
Reply to author
Forward
0 new messages