【问题标题】:How define multiple indexed fields in model class for mongodb collection in spring-data-mongodb如何在模型类中为spring-data-mongodb中的mongodb集合定义多个索引字段
【发布时间】:2015-08-09 14:11:31
【问题描述】:

我的模型类如下所示,我想要索引字段id、openid 和unionid。

但是在User集合中插入数据后,我发现只有一个索引字段id。

有没有办法通过spring-data-mongodb在模型类中指定多个索引字段?

public class User {
    @Indexed
    String id;
    @Indexed(unique = true)
    String unionid;
    @Indexed(unique = true)
    String openid;
    String nickname;
}

【问题讨论】:

    标签: spring mongodb spring-data-mongodb


    【解决方案1】:

    我发现 CompoundIndex 能够满足我的要求。

    @CompoundIndexes({ @CompoundIndex(name = "openid", unique = true, background = true, def = "{'openid': 1}"),
            @CompoundIndex(name = "unionid", background = true, def = "{'unionid': 1}") })
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2019-06-18
      • 2018-12-17
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多