如何在MongoDB中获取TTL或过期的文档?

发布于 2025-01-29 15:37:55 字数 83 浏览 1 评论 0原文

我想创建票务系统。在给定时间段内,在哪里取消票证?。但是,在它到期或在该特定机票到期之前,我想检验或将其保存在不同的集合中以备将来使用MongoDB吗?

I want to create ticketing system . Where ticket get cancelled after given period of time ?.For deleting after some time I am going to use indexing feature by MongoDb . But before it gets expire or after the expiry of that particular ticket I want to retrive or save it in different collection for future is it possible using mongodb ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-02-05 15:37:55

在当前版本的MongoDB中,我写这篇文章时,它并不直接支持,但它将在mongodb 6.0中(cf this jira票),在此期间,您可以使用一个解决方法(继续阅读!)。

让我解释一下。您要做的是:

  • 设置 ttl index 当时间通过x秒时,这将自动删除mongoDB集合中的文档。
  • 设置A 更改流中在此集合上使用过滤器集,以仅保留DELETE操作。

在5.0.8中,此变更流事件将仅包含已删除文档的_id字段,而没有其他内容,因为这是OPLOG中当前可用的唯一信息。

在6.0中,您将能够访问本文档的先前状态(因此在删除之前是最后一个状态)。

话虽这么说,帕维尔·杜乔夫尼(Pavel Duchovny)在他的博客文章。您可以轻松地容纳他的通知系统以实现您所需的行为。

In the current version of MongoDB, 5.0.8 as I'm writing this, it's not directly supported, but it will be in MongoDB 6.0 (CF this jira ticket) and there is a workaround that you can use in the meantime (keep reading!).

Let me explain. What you are trying to do is:

  • set up a TTL index that will remove automatically the docs in your MongoDB collection when the time is passed by X seconds.
  • set up a Change Streams on this collection with a filter set to only keep delete operations.

In 5.0.8, this change stream event will only contain the _id field of the deleted document, and nothing else, as that's the only information currently available in the oplog.

In 6.0, you will be able to access the previous state of this document (so it's last state before being deleted).

That being said, there is a workaround that Pavel Duchovny explained in his blog post. You can easily accommodate his notification system to achieve your desired behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文