I knew I could use expireAfterSeconds in Mongoose to set the data to expire/remove after a specific time, but what if I have a timestamp, I want the data to expire after the timestamp.
I added expireAt into my schema object;
expireAt: {
type: Date,
expires: 1,
},
Although in Mongodb, it exists an expireAt field appears, it didn't expire at the datetime in the object like: expireAt: 2023-06-01T04:04:32.000+00:00
(The similar question in how-to-set-data-expire-time-use-pymongo didn't solve)
Also, is it possible to set certain conditions, when it is filled, deleteMany to the database?
We can Expire Documents at a Specific Clock Time.
We need to create an index on the collection's expireAt field and specify the expireAfterSeconds value of 0.
For mongoose, you can create the TLL index on schema level or path level.
import mongoose from "mongoose";
import { config } from '../../config';
mongoose.set('debug', true);
const modelSchema = new mongoose.Schema({
expireAt: Date
});
// TTL index on schema level
modelSchema.index({ expireAt: 1 }, { expireAfterSeconds: 0 })
// Or
// path level
// mongo TTL index 'expireAfterSeconds' value will take 'expires' value expressed in seconds.
// const modelSchema = new mongoose.Schema({
// expireAt: { type: Date, expires: 0 }
// });
const Model = mongoose.model('model', modelSchema);
(async function main() {
try {
await mongoose.connect(config.MONGODB_URI);
// seed
const baseTime = new Date().getTime();
await Model.create([{ expireAt: baseTime + 2 * 1_000 }])
} catch (error) {
console.error(error);
} finally {
await mongoose.connection.close()
}
})();
Note Timing of the Delete Operation
The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time that a document expires and the time that MongoDB removes the document from the database.
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. MongoDB starts deleting documents 0 to 60 seconds after the index completes.
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.
So you need to wait at least 60 seconds even though the document expires at 2 seconds later from now on, then check the document in the collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With