Automatically update booking status in MongoDB (Mongoose, Node.js) when start time is reached. #169664
-
|
How can I automatically update a booking’s status in MongoDB (Mongoose, Node.js) when its start time arrives? Each booking document has: startTime (Date) status (e.g., "accepted", "inProgress", "completed") Example: If a booking’s status is "accepted" and its startTime is 2 days ahead, when that time arrives, I want the status to automatically change to "inProgress". I’m using JavaScript with Mongoose. What’s the best approach to make this update happen automatically without manual triggers? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
MongoDB won’t update documents automatically at a specific time by itself. |
Beta Was this translation helpful? Give feedback.
-
|
Hello, @sirajahmedx . // Run every minute }); |
Beta Was this translation helpful? Give feedback.
-
|
MongoDB won’t auto-update docs at a future time — your Node.js app must handle it. Quick solution (small/medium apps) – use node-cron to check every minute: Better for precise/large-scale – use a job scheduler like Agenda or Bull to run exactly at startTime and persist jobs across restarts. Tip: If you only need to display "inProgress" when startTime passes, compute it at read-time instead of storing it. |
Beta Was this translation helpful? Give feedback.
MongoDB won’t update documents automatically at a specific time by itself.
You need to handle it from your Node.js app.
Simplest way is to use a scheduler like node-cron to run every minute (or your choice) and update bookings whose startTime has passed from "accepted" to "inProgress".
If you need more precise timing or have a lot of bookings, use a job queue like Agenda or Bull to schedule the update exactly at the booking’s start time.
For very simple cases, you could also just check the time when reading the booking and return "inProgress" if the start time has already passed, but this won’t actually change the stored value in the database.