As of the current MongoDB version, which is 5.0.8 at the time of writing this, direct support for your specific task is not available. However, it is expected to be included in MongoDB 6.0 (refer to this jira ticket). In the meantime, there is a workaround that you can utilize.
To elaborate, what you are aiming to achieve involves:
- Configuring a TTL index to automatically remove documents from your MongoDB collection after a specified amount of time.
- Establishing Change Streams on this collection with a filter set to capture delete operations exclusively.
In version 5.0.8, the change stream event will only include the _id
field of the deleted document as that is the sole information currently present in the oplog.
With version 6.0, you will gain access to the previous state of the deleted document.
Nevertheless, there exists a workaround detailed by Pavel Duchovny in his blog post. By implementing his notification system, you can effectively achieve your intended functionality.