我知道有类似的问题,例如:Spring boot / mongo wont create index with the index comment
\nGithub 中的问题还包括“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
标签。
那么,我尝试了什么?
\nspring.data.mongodb.auto-index-creation: true
根据其他答案,我做的第一件事就是以这种方式添加:
@CreatedDate\n@Indexed(name="timestamp_index", expireAfterSeconds=100))\nprivate Date timestamp;\n
Run Code Online (Sandbox Code Playgroud)\n这不起作用......但我也读到问题可能出在我上课时AbstractMongoClientConfiguration
。
目前该项目没有该类,但存在MongoConfiguration
带有该@Configuration
标签的类。我不知道这是否会干扰或其他什么。
类是这样的:
\nspring:\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
所以我尝试在这里手动添加自动索引为 …