How to create a composite unique index in Morphia / MongoDB Java?

Let's say I have a MongoDB object that looks like this:

@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 so that the pair is manufacturer,model,yearunique.

When I try the following annotation - @Indexes(@Index(value="manufacturer,model,year, unique=true))- it works. But this causes 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=[], options=@org.mongodb.morphia.annotations.IndexOptions(unique=false, dropDups=false, background=false, name=, expireAfterSeconds=-1, disableValidation=false, language=, languageOverride=, sparse=false))

How to configure the index?

+4
source share
1 answer

Try 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;
}
+10
source

Source: https://habr.com/ru/post/1623930/


All Articles