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

标签 spring mongodb spring-boot spring-mongodb mongodb-indexes

我对 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/

相关文章:

java - 如何简化和减少具有特定日期格式的 SQL 查询的执行时间?

mongodb - 使用 Testcontainers + Quarkus + MongoDB 进行集成测试

java - Spring Boot、安全登录无限查询

java - 无法在此 ManagedType 上找到具有给定名称 [trackers] 的属性

java - 如何在单个 spring 容器中加载多个 jax-ws Web 服务

node.js - Mongo 使用 req.query.id Node 查找 id

java - mongodb - 如何使用 java 驱动程序构建多运算符更新语句?

java - 如何在Java spring Controller 中获取XML

spring-boot - 与在 Kubernetes 集群上使用直接 RSocket 应用程序通信相比,Netifi 代理有何改进?

java - Spring Web 服务流程