Spring Boot MongoDB 索引为 expireAfterSeconds 以自动删除文档不起作用

iSm*_*Smo 5 spring mongodb mongodb-indexes spring-boot spring-mongodb

我对 MongoDB 中的“生存时间”设置有疑问。我在我的实体中的 Spring-Boot 2.0.2.RELEASE 项目中创建了一个索引注释,它代表我在 MongoDB 中的文档。我将测试的“expireAfterSeconds”设置为 15 秒,但 MongoDB 在 15 秒后不会删除插入的文档。有人能告诉我我做错了什么吗?

这是 JSON 形式的 MongoDB 索引:

[
  2,
  {
    "createdDateTime" : 1
  },
  "deleteAt",
  "AccountServiceDB.AccountRegistration",
  NumberLong(15)
]
Run Code Online (Sandbox Code Playgroud)

这是我的实体:

@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;
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

解释如下:“删除过期文档的后台任务每 60 秒运行一次。因此,在文档过期和后台任务运行之间的时间段内,文档可能会保留在集合中。”