Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a compound unique index in Morphia/MongoDB Java?

Let's say I have a MongoDB Entity that looks as follows:

@Entity(value = "car")
public class Car {
   public String manufacturer;
   public String model;
   public String year;
   public Double price;
}

I would like to add an index such that the pair manufacturer,model,year is unique.

When I try the following annotation -- @Indexes(@Index(value="manufacturer,model,year, unique=true)) -- it works. But it throws the following error:

[WARN] (main) : DatastoreImpl - This index on 'Car' is using deprecated configuration options.  Please update to use the fields value on @Index: @org.mongodb.morphia.annotations.Index(unique=true, dropDups=false, background=false, name=, value=manufacturer, model, year, expireAfterSeconds=-1, disableValidation=false, sparse=false, fields=[], [email protected](unique=false, dropDups=false, background=false, name=, expireAfterSeconds=-1, disableValidation=false, language=, languageOverride=, sparse=false))

How do I configure the index properly?

like image 846
RainSear Avatar asked Jan 12 '16 12:01

RainSear


1 Answers

Try the following annotation

@Entity(value = "car")
@Indexes(@Index(fields = { @Field("manufacturer"), @Field("model"), @Field("year") }, options = @IndexOptions(unique = true)))
public class Car {
   public String manufacturer;
   public String model;
   public String year;
   public Double price;
}
like image 105
chridam Avatar answered Sep 19 '22 15:09

chridam