S
S
Stepgor2021-01-19 12:49:47
MongoDB
Stepgor, 2021-01-19 12:49:47

How can I delete temporary information in the database?

What are the schemes for working with temporary information in the database?
For example, the user object has a SUBSCRIPTION field - it is an array of objects with id, name and expire properties. Expire is the unix time in seconds, which is calculated by Date.now() + SUBSCRIPTION TIME
To clean the database from inactive subscriptions, I came up with 2 methods:
1. In the first request to the server, let's say get_user, we compare expire with the time now, if less - delete and save the user.
2. This is a background script that runs through users and also compares and deletes, saves.
These methods can be combined

. Are there any other ways to process such "temporary" information?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-01-19
@Stepgor

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.

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question