Spring Data MongoDB如何以编程方式分配过期时间

Spring Data MongoDB如何以编程方式分配过期时间,mongodb,spring-data,Mongodb,Spring Data,我在任何Spring数据文档中都找不到,在MongoDB中为文档分配过期时间的方法是什么?您可以使用@index注释的expireAfterSeconds属性在类型为Date的字段上指定过期时间。大致如下: @Document public class SomeEntity { String id; @Field @Indexed(name="someDateFieldIndex", expireAfterSeconds=3600) Date someDate

我在任何Spring数据文档中都找不到,在MongoDB中为文档分配过期时间的方法是什么?

您可以使用
@index
注释的
expireAfterSeconds
属性在类型为
Date
的字段上指定过期时间。大致如下:

@Document
public class SomeEntity {

    String id;

    @Field
    @Indexed(name="someDateFieldIndex", expireAfterSeconds=3600)
    Date someDateField;

   // rest of code here

}
或者通过操作
MongoTemplate

mongoTemplate
    .indexOps(SomeEntity.class)
    .ensureIndex(new Index().on("someDateField", Sort.Direction.ASC).expire(3600));

谢谢,但是整个文档是否已过期并被删除,或者只是 场

根据MongoDB文档 TTL索引用于从集合中删除文档

因此,将删除整个文档,而不是唯一索引字段

注意:索引必须定位在日期字段上,否则TTL将不适用


关于

谢谢,但是整个文档是否过期并被删除,或者只是字段?始终是整个文档