Why mongodb doesn't delete document every 30 seconds?

378 views Asked by At

I have a problem with implementing TTL with typegoose for mongodb.. Basically I want to delete a document from the collection if it's older than 30 seconds.

@ObjectType("TokenResetPasswordType")
@InputType("TokenResetPasswordInput")
@index(
   { createdAt: -1, confirmed: 1 },
   { expireAfterSeconds: 30, partialFilterExpression: { confirmed: false } }
)
export class TokenResetPassword {
    @Field()
    @Property({ lowercase: true, required: true, unique: true })
    email: string;

    @Field(() => [User], { nullable: true })
    @Property({ ref: "User", default: "" })
    user?: Ref<User>;

    @prop({ default: Date.now, index: true })
    createdAt?: Date;
}
2

There are 2 answers

1
deceze On BEST ANSWER

https://docs.mongodb.com/manual/core/index-ttl/

TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time.

You need to create an expireAfterSeconds index for the createdAt field alone, not for two fields at once.

Also note:

https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation

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 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.

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.

0
Shubham Verma On

If you want to delete the record from monogoDB, this is how you need to create the schema:

You need to add index({ createdAt: 1 }, { expireAfterSeconds: 0 }) in your code while d efiningschema as below.

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema({ timestamps: true, versionKey: false })
export class OTP extends Document {

 @Prop({ type: String, required: true })
 otp: string;

 @Prop({ type: String, required: true })
 referenceNo: string;


 @Prop({ type: Date, expires: 300 }) // 300 seconds = 5 minutes
 expiresAt: Date;
}

export const OTPSchema = SchemaFactory.createForClass(OTP).index({ createdAt: 1 }, { expireAfterSeconds: 0 });

This will delete your record after 300 seconds once created.