问题
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
回答1:
Try removing your current indexes before you create the new ones.
If you're worried about production downtime etc for these indexes, you could:
- Add a second index just like the one you have on production now with a different name.
- delete the existing one
- restart the server so that the index in your Java code is created as expected
- delete your duplicate index.
回答2:
Will Shaver's answer is very good, however doesn't actually address the issue the gravetii is highlighting.
createIndex
will fail with this error if you are trying to create an index on the same fields, with the same order, but with different options. This is because the indexes options may cause the index have very different properties. This is obvious when you consider an index with the unique
flag.
If you call createIndex
with the same fields, the same order, and the same options, it will behave as you expect; making no change to the collection's indexes, and it will not throw an exception.
来源:https://stackoverflow.com/questions/27288373/index-already-exists-with-different-options-error-while-using-createindex-in-l