24

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"
}

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
}

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

@Indexed
protected String status;

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

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

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

faizi
  • 1,265
  • 2
  • 12
  • 28
  • Not exactly related to this issue, but it's the same error message, so might be useful for people searching: You can't add two TEXT indexes to a collection with different parameters. There can only be one (though it can reference multiple fields). See https://docs.mongodb.com/manual/core/index-text/ – naught101 Nov 06 '20 at 00:22

3 Answers3

25

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

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.

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

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
  • Your guess is correct. Dropped the index on code and removed duplicates from code and then rebooting the application fixed the issue. Thank You :) – faizi May 14 '15 at 12:04
  • I downgraded the version of my Ruby MongoDB library, and suddenly all my indexes with "expireAfterSeconds" already existed with different options, so I had to drop them all. – Hubro Jul 12 '16 at 08:00
4

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

mherBaghinyan
  • 476
  • 5
  • 13
  • This worked, but make sure you don't need the data inside the collection or copy it somewhere then restore it! – Mosheer Oct 25 '21 at 21:20
1

Attempting to create multiple compound text indexes can also cause this error.

Only one text index for text search is allowed per collection per the docs.

In a multitenant database, we wanted to enforce uniqueness within each customer's data by using compound indexes.

db.users.createIndex( { customer: "text", email: "text"}, { unique: true } );
db.users.createIndex( { customer: "text", cell: "text"}, { unique: true } );

Doing this caused the op's error.

However, changing them to regular indices resolved the issue.

db.users.createIndex( { customer: 1, email: 1}, { unique: true } );
db.users.createIndex( { customer: 1, cell: 1}, { unique: true } );
David
  • 895
  • 8
  • 12