Index already exists with different options error while using createIndex() in latest MongoDB java driver

gravetii picture gravetii · Dec 4, 2014 · Viewed 11.4k times · Source

So I am upgrading the MongoDB java driver to 2.12.4 where the ensureIndex() method has been deprecated. I am instead using the createIndex() method which from the docs seems like to funciton similarly to ensureIndex(). However, when I use this method in production, I get the following error -

{ "serverUsed" : "X.X.X.X" , "ok" : 0.0 , "errmsg" : "Index with name: <index_name> already exists with different options" , "code" : 85}

Why does this happen? Could anyone help me out with this?

Thanks

Answer

Will Shaver picture Will Shaver · Dec 4, 2014

Try removing your current indexes before you create the new ones.

If you're worried about production downtime etc for these indexes, you could:

  1. Add a second index just like the one you have on production now with a different name.
  2. delete the existing one
  3. restart the server so that the index in your Java code is created as expected
  4. delete your duplicate index.