Search code examples
javascriptmongodbcollectionsauto-updatedatabase-server

Mongodb server. How to automatically change the status is expired when expired


In mongodb I have a collection (user), where there is 2 information is status and expiry (date). How to automatically change the status is expired when expired at database server. Thanks


Solution

  • You can automatically delete documents using TTL indexes if that helps and/or listen/react to changes using change streams which have also been widely discussed here: How to listen for changes to a MongoDB collection?

    Above and beyond this, however, there's nothing built into MongoDB at this stage that could be used to achieve this kind of behaviour so you will have to build something yourself using e.g. an external scheduler that runs an update command on a regular basis setting the desired values on the right documents.