1

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

ThuanIT
  • 33
  • 7
  • 1
    While I'm unfamiliar with MongoDB in specific and can't seem to find anything like this in the documentation, when it comes to data processing in general you don't really want to store calculated field data like that - it's needlessly redundant. If you're looking for an easy way to exclude expired records from a query, you should query for records whose expiry date is later than the time the query is executed. If you're looking for human-readability, your application's UI should have logic to display an "active" or "expired" label based on the expiry date in the data set. – p0lar_bear Dec 03 '18 at 19:16

1 Answers1

0

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.

dnickless
  • 10,733
  • 1
  • 19
  • 34