I’m using Nestjs with Mongodb and want to delete record automatically from api-hit logs collection after the TTL is expired. I know how to configure the schema for auto – deletion of records using expiresAfterSeconds but the scenario is:
- Each record in the collection will have different TTL.
- The TTL property for each record is present in another collection.
- The common identifier for e.g. is ‘username’ property in both collections.
TTL configuration with constant ‘expireAfterSeconds’.
Const mongoose = require(‘mongoose’)
Const userSchema = new mongoose.Schema({
{
createdOn: {
type: Date,
required: true
},
.
.
.
}, {timestamps: true})
**userSchema.index({createdOn: 1}, {expireAfterSeconds: 3600});**
But what I want is to configure ‘expireAfterSeconds’ dynamically something like below (Each record should have different TTL)
**userSchema.index({createdOn: 1}, {expireAfterSeconds: dynamicTTL});**
Kindly provide me solution for the above dear fellow developers!
Udayaprakash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.