本文介绍了MongoException:带有名称的索引:代码已经存在,具有不同的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a mongodb collection term with following structure

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

和一个将术语集合表示为Term.java

and a java class representing the term collection as 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中添加了一个新字段,

I have many documents in term collection. Now I am added a new field to Term.java as

@Indexed
protected String status;

将字段status添加到Term.java之后,在向term集合中插入新术语的同时,我得到了exceptoin:

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

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

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

推荐答案

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

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

尝试:db.Term.getIndexes()

如果确实是这种情况(您在代码字段上具有非唯一索引),请发出:db.Term.dropIndex("code_1")(替换为代码字段索引名称).

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).

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

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

或者,从@Indexed批注中删除唯一属性(如果您不希望其唯一,则除外).

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

这篇关于MongoException:带有名称的索引:代码已经存在,具有不同的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:07