【问题标题】:Why @Indexed is not creating an index in MongoDB? (MongoDB, Morphia, Spring Data)为什么@Indexed 不在 MongoDB 中创建索引? (MongoDB、Morphia、Spring Data)
【发布时间】:2020-03-22 20:05:41
【问题描述】:

为什么 Morphia 的 @Indexed 注解没有在 Mongo DB 中创建索引?

  //@Indexed(name = "_ts", expireAfterSeconds = 7200) //This was written earlier which was not working and it is deprecated too

    @Indexed(options = @IndexOptions(name = "_ts", expireAfterSeconds = 20))
    public Date _ts = new Date(new Date().getTime() + 1000 * 60 * 60 * 4);

创建索引需要单独的代码吗?

Mongo DB 版本:3.2.22

(我使用过 ORM 工具 - hibernate,其中我们将 ddl proeprty 设置为 true,自动创建表,morphia 也有相同的)

【问题讨论】:

    标签: mongodb spring-data-mongodb morphia


    【解决方案1】:

    您需要在您的 Datastore 参考上致电 ensureIndexes()

    【讨论】:

      【解决方案2】:

      在属性文件中:

      spring.data.mongodb.auto-index-creation: true
      

      https://stackoverflow.com/a/62655088/2266070

      【讨论】:

        猜你喜欢
        • 2019-06-04
        • 2016-06-25
        • 2019-03-10
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-08
        相关资源
        最近更新 更多