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

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)

  • 谢谢,但是整个文档都过期并被删除还是只是字段过期并被删除? (2认同)
  • 它始终是整个文档 (2认同)