我对 MongoDB 中的“生存时间”设置有疑问。我在实体的 Spring-Boot 2.0.2.RELEASE 项目中创建了一个索引注释,它代表 MongoDB 中的文档。我将测试的“expireAfterSeconds”设置为 15 秒,但 MongoDB 在 15 秒后不会删除插入的文档。有人可以告诉我我做错了什么吗?
这是 JSON 格式的 MongoDB 索引:
[
2,
{
"createdDateTime" : 1
},
"deleteAt",
"AccountServiceDB.AccountRegistration",
NumberLong(15)
]
这是我的实体:
@Document(collection = "AccountRegistration")
public class UserRegistration {
@Id
private ObjectId _id;
@Indexed(unique = true)
private String username;
@Indexed(unique = true)
private String email;
private String user_password;
@Indexed(name = "deleteAt", expireAfterSeconds = 15)
private Date createdDateTime;
public UserRegistration() {}
public ObjectId get_id() {
return _id;
}
public void set_id(ObjectId _id) {
this._id = _id;
}
}
最佳答案
Here解释是:“删除过期文档的后台任务每 60 秒运行一次。因此,在文档过期和后台任务运行期间,文档可能会保留在集合中。”
关于Spring Boot MongoDB 用 expireAfterSeconds 索引自动删除文档不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533151/