如何在模型 class 中为 spring-data-mongodb 中的 mongodb 集合定义多个索引字段
How define multiple indexed fields in model class for mongodb collection in spring-data-mongodb
我的模型 class 如下所示,我想要索引字段 id
、openid
和 unionid
。
但是在 User
集合中插入数据后,我发现只有一个索引字段 id
。
有没有办法通过使用 spring-data-mongodb 在模型 class 中指定多个索引字段?
public class User {
@Indexed
String id;
@Indexed(unique = true)
String unionid;
@Indexed(unique = true)
String openid;
String nickname;
}
我发现 CompoundIndex 能够满足我的要求。
@CompoundIndexes({ @CompoundIndex(name = "openid", unique = true, background = true, def = "{'openid': 1}"),
@CompoundIndex(name = "unionid", background = true, def = "{'unionid': 1}") })
我的模型 class 如下所示,我想要索引字段 id
、openid
和 unionid
。
但是在 User
集合中插入数据后,我发现只有一个索引字段 id
。
有没有办法通过使用 spring-data-mongodb 在模型 class 中指定多个索引字段?
public class User {
@Indexed
String id;
@Indexed(unique = true)
String unionid;
@Indexed(unique = true)
String openid;
String nickname;
}
我发现 CompoundIndex 能够满足我的要求。
@CompoundIndexes({ @CompoundIndex(name = "openid", unique = true, background = true, def = "{'openid': 1}"),
@CompoundIndex(name = "unionid", background = true, def = "{'unionid': 1}") })