标签: mongo-index

Spring Boot 中未使用 @Indexed 创建 Mongo 索引

我知道有类似的问题,例如:Spring boot / mongo wont create index with the index comment

\n

Github 中的问题还包括“spring.data.mongodb.auto-index-creation=true”无法正常工作

\n

我还尝试过这个 Baeldung 指南:Spring Data MongoDB \xe2\x80\x93 Indexes, Annotations and Converters

\n

所以问题是我正在尝试使用 @Indexed 注释向现有集合添加索引,如下所示:

\n
@CreatedDate\n@Indexed(name="timestamp_index", expireAfterSeconds=100))\nprivate Date timestamp;\n
Run Code Online (Sandbox Code Playgroud)\n

该字段是在将元素插入数据库时​​自动创建的时间戳。

\n

该类也有@Document标签。

\n

那么,我尝试了什么?

\n

spring.data.mongodb.auto-index-creation: true根据其他答案,我做的第一件事就是以这种方式添加:

\n
@CreatedDate\n@Indexed(name="timestamp_index", expireAfterSeconds=100))\nprivate Date timestamp;\n
Run Code Online (Sandbox Code Playgroud)\n

这不起作用......但我也读到问题可能出在我上课时AbstractMongoClientConfiguration

\n

目前该项目没有该类,但存在MongoConfiguration带有该@Configuration标签的类。我不知道这是否会干扰或其他什么。

\n

类是这样的:

\n
spring:\n  data:\n    mongodb:\n      uri: ${env.mongo-database.url}\n      auto-index-creation: true\n
Run Code Online (Sandbox Code Playgroud)\n

这个类创建一个@Bean命名的mongodb所以我尝试在这里手动添加自动索引为 …

java mongodb spring-boot mongo-index

5
推荐指数
0
解决办法
2715
查看次数

标签 统计

java ×1

mongo-index ×1

mongodb ×1

spring-boot ×1