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

- 14,010
- 29
- 101
- 161

- 2,519
- 3
- 18
- 23
-
3MongoDb documentation is actually very good. Did you try? – Jayan Sep 22 '14 at 07:42
5 Answers
Update 2: The original answer, as well as the first update, erroneously reference the Mongo shell documentation instead of the 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.
Update:
db.collection.ensureIndex()
is deprecated since version 3.0.0.
Is now an alias for db.collection.createIndex().
Original:
createIndex()
is deprecated since 1.8
It was used to create indexes on collections whereas ensureIndex()
creates an index on the specified field if the index does not already exist. 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
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.

- 273
- 1
- 6

- 15,233
- 5
- 34
- 34
-
17According to [mongodb docs](http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/) `ensureIndex()` is deprecated. – KPrince36 Mar 18 '15 at 19:27
-
1`createIndex` will soft-fail in fact (at least at 3.0). On subsequent calls it will return "ok", but also "note" stating that "all indexes already exist" – PeterM Mar 30 '17 at 07:26
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.
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 DBCollection Java API anymore.

- 1,478
- 1
- 22
- 28

- 273
- 1
- 6
-
Btw, I can't comment on @sol4me 's wrong answer due to lack of reputation, nor resurrect my wrongly deleted original answer, or do anything much else for that matter. :-( – Jens Grivolla Oct 16 '14 at 11:26
-
If you think you are right, post well-written answer, not that kind of useless answer. This is going to be deleted for sure. – Dici Oct 16 '14 at 11:26
-
2I'm sorry, but what is useless about my answer? I am saying that @sol4me got it wrong, and in the Java API you actually have to use createIndex(). I even reference the corresponding JIRA issue where I managed to clarify this with the MongoDB developers... If I ever got a reply from those who delete my answer I would edit it and not leave all the stuff in there to show how it got deleted. As it is, this is all extremely hostile towards me, after I even made the effort of discussing things with the developers so I could give a correct answer. – Jens Grivolla Oct 16 '14 at 11:30
-
You are quoting other comments and arguing instead of simply answering the question. Do as you wish, I just wanted to tell you there is no way for that kind of answer to stay more than a couple of hours – Dici Oct 16 '14 at 11:31
-
My correct answer got deleted twice, without any explanation or any reaction to my request for clarification, so yes, I am seriously pissed and that shows in the repost of my answer. If anybody had the decency to treat me like a human being instead of just deleting my answers (I had neither quoted anybody nor argued at that point), this thread wouldn't exist. – Jens Grivolla Oct 16 '14 at 11:37
-
I have now removed my comments about the deletion from the answer. However, I still find it extremely disrespectful to just delete my *correct* answer without comment and without responding to my requests for clarification. @bluefeet why did you delete my post? I already explained that this is the correct answer, all other answers are WRONG. You could at least reply when I ask about it instead of just deleting it again and the related comments. I actually point to this question in the MongoDB JIRA issue, and you just keep deleting my answer, which is THE ONLY CORRECT ONE. – Jens Grivolla Oct 16 '14 at 13:09
-
You really are an annoying one. People who review low quality answers and decide to delete it treat tens of posts a day. There's nothing personal against you, and they probably don't even remember you. A message is deleted only if several reviewers considered it had to. If you disagree, you can repost it trying to improve your answer. If it is deleted again, wonder why your answers are systematically deleted and stop accusing the others. If you can't change your mentality, just don't participate to SO, which is about cooperation and no competition. – Dici Oct 16 '14 at 16:07
-
3Ok, I guess this is it then. My correct and documented answer got deleted twice, scored down twice, while the other answer which is the exact opposite of the correct answer is at +3. Clearly there is not much I can do about it. Thanks @Dici for at least replying. Btw, I'm replying here because I lack reputation to move the discussion to chat as suggested by SO, which is also why my initial answer was not a comment on the existing answer. – Jens Grivolla Oct 17 '14 at 07:54
The ensureIndex
method 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.
For this reason, the java driver implemented a createIndex
method with identical behaviour, except it won't cache the index status.
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.
As to why they did not change the behaviour without renaming - that I have no idea.

- 4,217
- 2
- 20
- 33
Deprecated since version > 3.0.0: db.collection.ensureIndex() is now an alias for db.collection.createIndex().

- 21
- 2
Removed in 5.0
db.collection.ensureIndex()
has been replaced by db.collection.createIndex()
.
Snapshot:

- 8,783
- 6
- 58
- 79