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;
}
https://docs.mongodb.com/manual/core/index-ttl/
You need to create an
expireAfterSeconds
index for thecreatedAt
field alone, not for two fields at once.Also note:
https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation