Issue
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;
}
Solution
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.
Answered By - deceze
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.