'Why Spring Data ensureIndex.expire does not work with ReactiveMongoTemplate

I have a Spring-Boot application and MongoDB as database.

I have an entity:

@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";
    private static final String CREATED_AT = "created_at";



    @Id
    private PersonId id;

    @Field(name = PERSON_NAME)
    private String personName;

    
    @Field(name = CREATED_AT)
    private LocalDateTime createdAt;
}

I have a repository:

@Slf4j
@Repository
public class PersonRepositoryImpl implements PersonRepository {

    private final int expireAfterSeconds;
    private final ReactiveMongoTemplate mongoTemplate;

    public SellerRequestInfoCustomRepositoryImpl(@Value("${ttl.index}") int expireAfterSeconds, ReactiveMongoTemplate mongoTemplate) {
        this.expireAfterSeconds = expireAfterSeconds;
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public void saveWithTtl(PersonEntity entity) {
        mongoTemplate.indexOps(PersonEntity.COLLECTION_NAME)
                .ensureIndex(new Index().on(PersonEntity.CREATED_AT, ASC)
                        .expire(expireAfterSeconds));
        mongoTemplate.save(entity).subscribe(result -> log.info("Entity has been saved: {}", result));
    }
}

And I have an application.yml:

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

...
ttl:
  index: 20

But, when I save the entity from component like:

 sellerRequestInfoCustomRepository.saveWithTtl(entity);

The document is been creating in mongodb, but the ttl index NOT:

screenshot

What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source