简体   繁体   English

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

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

Let's say I have a MongoDB Entity that looks as follows: 假设我有一个MongoDB实体,如下所示:

@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. 我想添加一个索引,使得对manufacturer,model,year是独一无二的。

When I try the following annotation -- @Indexes(@Index(value="manufacturer,model,year, unique=true)) -- it works. But it throws the following error: 当我尝试以下注释 - @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))

How do I configure the index properly? 如何正确配置索引?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM