为什么使用Spring Data时,Mongo TTL索引没有被创建?

3
问题:Spring Data 未能创建 MongoDB TTL 索引
我有一个使用 Spring Boot 的应用程序,使用 MongoDB 作为数据库。
我有一个实体类:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(COLLECTION_NAME)
public class PersonEntity {

    public static final String COLLECTION_NAME = "person_info";
    private static final String PERSON_NAME = "person_name";


    @Id
    private PersonId id;

    @Field(name = PERSON_NAME)
    private String personName;

    
    @Indexed(name = "ttl_index", expireAfterSeconds=20)
    private LocalDateTime date;
}

我使用这个仓库将一个新的文档持久化到MongoDB中:
public interface SellerRequestInfoRepository extends ReactiveMongoRepository<PersonEntityEntity, PersonId> {}

使用
personRepository.save(entity);

但是在插入Person文档后,MongoDB上没有创建TTL索引: 截图 请指导我做错了什么。

请澄清您的具体问题或提供额外的细节,以准确突出您所需的内容。目前的描述不够清晰,很难确定您在询问什么。 - Community
我在这个问题上添加了更多细节。 - Anni Benni
1个回答

2
我引用了 文档

Spring Data MongoDB 可以自动为使用 @Document 注解的实体类型创建索引。从版本 3.0 开始,必须显式启用索引创建以防止对集合生命周期和性能造成不良影响。

您是否启用了自动索引创建?如果没有启用,则很可能是您的索引未被创建的原因。

使用 Spring Boot,您可以通过以下方式在您的 application.yml 中启用自动索引创建:

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

或者,如果您更喜欢属性:

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

1
谢谢!这就是我错过的:application.yaml配置文件。 - Anni Benni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接