[英]How do I create a compound unique index in Morphia/MongoDB Java?
假設我有一個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))
如何正確配置索引?
請嘗試以下注釋
@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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.