I have a requirement where I need to remove some records from a collection. Let’s see how to do that in Mongoose. I use Mongoose: 5.4.9 and MongoDB v4.0.3.
Here are the keys:
- It needs to be a
- It needs to setup a
- You can use
defaultto setup the start time.
Take the above setup for example. The default value for
expireAt field will be
Date.now, when you create this record, you don’t need to assign value for
expireAt, so it will be assigned with
Date.now. Then we setup it will expire after
5m. Which means:
After 5 minutes from creation, this record will be removed from Database.
5m is a shortcut here, you can refer to this package to check how to use words rather than number to make the setup more readable.
If you change the
expires, you might need to drop the original index, otherwise, it won’t just work.
It is about the concept of
TTL in MongoDB: Time-To-Live. According to the official document:
The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.
Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.
Hop it helps.