Spring Data MongoDB如何以编程方式分配到期时间 [英] Spring Data MongoDB how to assign expiration time programmatically
本文介绍了Spring Data MongoDB如何以编程方式分配到期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在任何Spring-Data文档中都找不到,在MongoDB中为文档分配到期时间的方法是什么?
I couldn't find in any of the Spring-Data documents, what is the way to assign expiration time to a document in MongoDB?
推荐答案
您可以在类型为Date
的字段上使用@Indexed
批注的expireAfterSeconds
属性来执行此操作.大致:
You can do it using @Indexed
annotation's expireAfterSeconds
attribute over a field whose type is Date
.Roughly:
@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));
这篇关于Spring Data MongoDB如何以编程方式分配到期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文