問題:沒有通過 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 索引:
截屏
請,建議,我做錯了什么
uj5u.com熱心網友回復:
我參考檔案:
Spring Data MongoDB 可以自動為帶有 @Document 注釋的物體型別創建索引。從 3.0 版開始,必須顯式啟用索引創建,以防止對集合生命周期和性能造成不良影響。
您是否啟用了自動索引創建?因為如果您不這樣做,那很可能是未創建索引的原因。
使用 Spring Boot,您可以使用以下配置啟用自動索引創建application.yml:
spring:
data:
mongodb:
auto-index-creation: true
或者,如果您更喜歡屬性:
spring.data.mongodb.auto-index-creation=true
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/475952.html
