mongodb MongoException:带有名称的索引:代码已经存在,具有不同的选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30232081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:26:16  来源:igfitidea点击:

MongoException: Index with name: code already exists with different options

mongodbspring-data-mongodbmongodb-javamongodb-indexes

提问by faizi

I have a mongodbcollection termwith following structure

我有一个具有以下结构的mongodb集合term

{
    "_id" : "00002c34-a4ca-42ee-b242-e9bab8e3a01f",
    "terminologyClass" : "USER",
    "code" : "X67",
    "terminology" : "some term related notes",
    "notes" : "some notes"
}

and a java class representing the term collection as Term.java

和一个表示术语集合的java类 Term.java

@Document
public class Term{  

    @Id
    protected String termId;

    @Indexed
    protected String terminologyClass;

    @Indexed(unique=true)
    protected String code;

    @Indexed
    protected String terminology;

    protected String notes;

    //getters & setters
}

I have many documents in termcollection. Now I am added a new field to Term.javaas

term收集了很多文件。现在我添加了一个新字段Term.java作为

@Indexed
protected String status;

After adding field statusto Term.java, while inserting a new term to termcollection I am getting an the exceptoin :

将字段添加status到 后Term.java,在向term集合中插入新术语时,我得到了一个异常:

com.mongodb.MongoException: Index with name: code already exists with different options

com.mongodb.MongoException:带有名称的索引:代码已经存在,具有不同的选项

I am using MongoDB version : 2.6.5 and spring-data-mongodb version : 1.3.2

我正在使用 MongoDB 版本:2.6.5 和 spring-data-mongodb 版本:1.3.2

回答by Ori Dar

You already have an index on that collection with the same name, but with a different definition. My guess is that your current code index is non-unique

您已经在该集合上有一个具有相同名称但具有不同定义的索引。我的猜测是您当前的代码索引是非唯一的

try: db.Term.getIndexes()

尝试: db.Term.getIndexes()

If this is indeed the case (you have a non-unique index over code field), issue: db.Term.dropIndex("code_1")(replace with the code field index name).

如果确实如此(您在代码字段上有一个非唯一索引),请发出:( db.Term.dropIndex("code_1")替换为代码字段索引名称)。

Next time you boot your application, it's supposed to work alright.

下次启动应用程序时,它应该可以正常工作。

Alternatively, remove the unique attribute from the @Indexedannotation (if you don't except it to be unique).

或者,从@Indexed注释中删除唯一属性(如果您不希望它是唯一的)。

回答by mherBaghinyan

have you tried to drop your collection and try again? usually there are many conflicts while applying new java mapping to existing mongodb collection

你有没有试过放弃你的收藏然后再试一次?通常在将新的 java 映射应用于现有的 mongodb 集合时会有很多冲突