So im making a verification code sorta thing, they get the code then they can verify. But, I want to make it so it expires in 5 min (so the db isnt filled with random stuff) after creation how would I go about doing that.
CodePudding user response:
You can add expireAt
as time-to-live parameter with createIndex
.
To create a TTL index, use the createIndex() method on a field whose value is either a date or an array that contains date values, and specify the expireAfterSeconds option with the desired TTL value in seconds.
db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )
Document will expire in 1 hour and will be deleted automatically by mongodb.
Check this documentation | TTL indexes
Update:
In mongoose you can this parameter directly in Schema constructor
const schema = Schema({ name: String, timestamp: Date, metadata: Object }, {
expireAfterSeconds: 86400
});