skip to Main Content

I have a database with users collection which contains a verified field in its schema. I want users that have not been verified 5min after account creation to be deleted. How can I do this?

I am already familiar with the expires option, but I am not sure how I can apply it conditionally.

2

Answers


  1. You can try partialFilterExpression.

    db.users.createIndex(
      {"createdDate": 1}, 
      {expireAfterSeconds: 300, partialFilterExpression: {verified: false}}
    );
    
    Login or Signup to reply.
  2. You can create a field expireAt with an index and use expireAfterSeconds and partialFilterExpression options:

    ...
    verified: { type: Boolean, default: false },
    expireAt: {
      type: Date,
      default: Date.now,
      index: {
        expireAfterSeconds: 300,
        partialFilterExpression: { verified: false }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search