3

I want to create a document which automatically gets deleted on specific time using mongoose in NodeJS.

Writing this as a new question since I can find only TTL based solutions, I need expiry time based.

1 Answer 1

3

MongoDB supports auto deleting documents. https://docs.mongodb.com/manual/tutorial/expire-data/

In order to use this in Mongoose you need to create an index on the collection using this syntax: MySchema.index( { "expireAt": 1 }, { expireAfterSeconds: 0 } );

You also need to create a field in schema named expireAt and add the time at which you want the document to expire while adding document in the collection.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to delete Particular record in specific time any idea about this ?? Every record delete time is different
After creating the above index just add a field named as expireAt in the record that you want to delete and set value of expireAt as the time at which you want to delete the record. Ex { name: a, expireAt: <time of deletion of a> }, { name: b, expireAt: <time of deletion of b> }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.