我有一个 mongodb
集合 term
具有以下结构
{
"_id" : "00002c34-a4ca-42ee-b242-e9bab8e3a01f",
"terminologyClass" : "USER",
"code" : "X67",
"terminology" : "some term related notes",
"notes" : "some notes"
}
和一个将术语集合表示为 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
}
我在 term
集合中有很多文档。现在我在 Term.java
中添加了一个新字段
@Indexed
protected String status;
将字段 status
添加到 Term.java
后,在向 term
集合插入新术语时,我得到一个异常:
com.mongodb.MongoException: Index with name: code already exists with different options
我使用的是 MongoDB 版本:2.6.5 和 spring-data-mongodb 版本:1.3.2
最佳答案
您已经在该集合上有一个同名但定义不同的索引。我的猜测是您当前的代码索引是非唯一的
尝试:
db.Term.getIndexes()
如果情况确实如此(您在代码字段上有一个非唯一索引),请发出:
db.Term.dropIndex("code_1")
(替换为代码字段索引名称)。
下次启动应用程序时,它应该可以正常工作了。
或者,从 @Indexed
注释中删除唯一属性(如果您不希望它是唯一的)。
关于mongodb - MongoException : Index with name: code already exists with different options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232081/