如何在Morphia/MongoDB Java中创建复合唯一索引?

Rai*_*ear 6 java mongodb morphia

假设我有一个MongoDB实体,如下所示:

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

我想添加一个索引,使该对manufacturer,model,year是唯一的.

当我尝试以下注释时@Indexes(@Index(value="manufacturer,model,year, unique=true))- 它有效.但它会引发以下错误:

[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))
Run Code Online (Sandbox Code Playgroud)

如何正确配置索引?

chr*_*dam 13

请尝试以下注释

@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;
}
Run Code Online (Sandbox Code Playgroud)