Ars*_*Fan 10 mongodb spring-data
我在任何Spring-Data文档中都找不到,在MongoDB中为文档分配过期时间的方法是什么.任何人都可以帮忙举个例子吗?谢谢.
Ori*_*Dar 12
您可以使用@Indexed注释的expireAfterSeconds属性对类型为的字段执行此操作.Date粗略地:
@Document
public class SomeEntity {
String id;
@Field
@Indexed(name="someDateFieldIndex", expireAfterSeconds=3600)
Date someDateField;
// rest of code here
}
Run Code Online (Sandbox Code Playgroud)
或者通过操纵MongoTemplate:
mongoTemplate
.indexOps(SomeEntity.class)
.ensureIndex(new Index().on("someDateField", Sort.Direction.ASC).expire(3600));
Run Code Online (Sandbox Code Playgroud)