简体   繁体   中英

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 . 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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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