简体   繁体   English

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

[英]How to get TTL or expired document in 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 .在给定的时间段后,票在哪里被取消?。为了在一段时间后删除,我将使用 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 ?但是在它到期之前或在该特定票证到期之后我想检索或将其保存在不同的集合中以备将来使用 mongodb 是可能的吗?

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!).在我写这篇文章的当前版本的 MongoDB 5.0.8 中,它不被直接支持,但它将在 MongoDB 6.0 中(CF this jira ticket )并且有一个你可以同时使用的解决方法(继续阅读!)。

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.设置一个TTL 索引,当时间经过 X 秒时,它将自动删除 MongoDB 集合中的文档。
  • 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.在 5.0.8 中,此更改流事件将仅包含已删除文档的_id字段,而没有其他内容,因为这是 oplog 中当前可用的唯一信息。

In 6.0, you will be able to access the previous state of this document (so it's last state before being deleted).在 6.0 中,您将能够访问此文档的先前状态(因此它是被删除之前的最后状态)。

That being said, there is a workaround that Pavel Duchovny explained in his blog post .话虽如此,Pavel Duchovny 在他的博客文章中解释了一种解决方法。 You can easily accommodate his notification system to achieve your desired behaviour.您可以轻松地适应他的通知系统来实现您想要的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM