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

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

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

我希望添加一个索引使得三个值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))

如何正确地配置索引?

1个回答

15

可以尝试以下注解

@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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接