koala1838
koala1838

Reputation: 47

How to automatically change property value in MongoDB when specific date is reached

Is it possible, when creating a document in mongo db, to set a date in the future, like 30 days from today, and then automatically change an other property of this document, when this date is reached?

For example:

Before 31.12.2020:

{ title: "Silvester Party", eventStart: 2020-12-31T23:00:00.000+00:00 over: false }

After 31.12.2020:

{ title: "Silvester Party", eventStart: 2020-12-31T23:00:00.000+00:00 over: true }

Upvotes: 2

Views: 1340

Answers (2)

mevrick
mevrick

Reputation: 1045

You can inbuilt MongoDB Atlas Triggers. You can also use a third-party library like mongodb-cron or node-cron.

Upvotes: 0

kennysliding
kennysliding

Reputation: 2967

MongoDB does not support a self-update field, that is, a field will not ever update itself without external instructions. So you will need to set up a cronjob that calls an execution to the database to modify the field.

For example, you can create a simple API that updates every field that has the matched date. And you can set up a cronjob to call this API once every day.

Upvotes: 1

Related Questions