Index already exists with various parameter errors when using createIndex () in the latest MongoDB java driver

So, I am updating the MongoDB java driver to 2.12.4, where the ensureIndex() method is deprecated. Instead, I use the createIndex() method, which from the documents looks like funciton similar to ensureIndex() . However, when I use this method in production, I get the following error -

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

Why is this happening? Can anyone help me with this?

thanks

+6
source share
2 answers

Try deleting current indexes before creating new ones.

If you are worried about production downtime, etc. for these indices, you can:

  • Add a second index, like the one you have in production. with a different name.
  • delete existing
  • restart the server so that the index in your java code is created as expected
  • delete the duplicate index.
+8
source

The answer to Shaver is very good, but it doesn't really address the issue that gravetii is highlighting.

createIndex will fail with this error if you try to create an index in the same fields with the same order but with different parameters. This is due to the fact that index parameters can lead to the fact that the index has very different properties. This is obvious if you are considering an index with a unique flag.

If you call createIndex with the same fields, the same order and the same parameters, it will behave as you expect; without changing the collection indexes, and will not throw an exception.

+4
source

Source: https://habr.com/ru/post/979091/


All Articles