Search code examples
mongodbmorphia

Convert existing index to unique (mongodb) using morphia


I have an existing index on a field using Morhia.

@Indexed
public String field;

now I want to convert that to a unique index.

@Indexed(unique = true)
public String field;

On application start, I call db.ensureIndexes() - however the uniqueness of the index is not reflected in mongo.

Why is this failing? Whats the best way to achieve this?


Solution

  • The properties of an index in MongoDB can't be modified after creation; thus you cannot directly convert a non-unique index into a unique index. You will have to drop the index manually before it can be recreated as a unique index. You can do this directly with the mongo shell: see http://www.mongodb.org/display/DOCS/Indexes#Indexes-DroppingIndexes.