Exploring the Power of MongoDB Atlas Scheduled Triggers

In this article, we’ll delve into the world of MongoDB Atlas scheduled triggers and explore how to configure them in your MongoDB Atlas account. Scheduled triggers allow you to execute a specific piece of code at regular intervals, providing a convenient way to automate tasks on a schedule.

We’ll guide you through the process of setting up these triggers, enabling you to run code automatically based on your defined schedule. Whether it’s updating data regularly, generating reports, or performing other periodic tasks, MongoDB Atlas scheduled triggers offer a powerful solution for streamlining your database management.

What are Atlas Scheduled Triggers?

Scheduled triggers allow you to execute server-side logic on a regular schedule that you define. You can use scheduled triggers to do work that happens on a periodic basis, such as updating a document every minute, generating a nightly report, or sending an automated weekly email newsletter.

Setting up Scheduled Trigger on Atlas

To create a scheduled trigger in the Atlas app services UI:

1. Click triggers under build in the left navigation menu.
2. Click add a trigger to open the trigger configuration page.
3. Select scheduled for the trigger type.

After that, you will see the configuration page for triggers as follows:

Add Trigger

Fig.1.1: Source

Schedule Type

Fig.1.2

Select an Event Type

Fig.1.3

Step 1. In trigger type select scheduled.

Step 2. Give a name to your trigger.

Step 3. Set enabled as active if you make it disabled then your trigger doesn’t run on scheduled time.

Step 4. Skip events on re-enable: If you enable this then your skipped events will run again you can choose this option accordingly.

Step 5. Schedule type: It is basically a periodic interval or specific time interval you provide to run a schedule trigger.

Step 6. Basic: In the basic section you can simply choose from the dropdown which monthly, daily or weekly interval you want it to be scheduled.

Step 7. Advance: Here you can give a cron command to schedule after certain time intervals.

Step 8. Then you can link from dropdown a data source i.e. Your MongoDB cluster where your database is hosted.

Step 9. Then to write a code you can write it as a function in the code block or use Amazon event bridge.

Step 10. After you write a code you can run your code & see the console output.

At the last, you can save your changes by clicking on SAVE Button.

Empower Your Project: Hire Skilled MERN Stack Developers Today!

Add Dependency for Atlas Triggers Code

Add Dependency

Fig.1.4

Click on the Add Dependency Button and then;

Visit Dependency

Fig.1.5

Write the package name exactly as it is on the npm website.

Example: If you want to add a package called sendgrid/mail. So you will need to enter the exact package name from the npm website. So your package name will be @sendgrid-mail.

SendGrid mail

Fig.1.6

Here I will give an example of how to add a package called @sendgrid-mail.

Add sendgrid package

Fig.1.7

This way you can add dependencies and import them into your code as follows;

const sendgrid = require(“@sendgrid-mail”)

Example of Sending Email Periodically Using Triggers

Suppose you want to send an email to your users on the 30th of every month then first select the appropriate interval.

Sending Email

Fig.1.8

After that, this will be the code you have to write in function code;

function code

Fig.1.9

This way you can extract your database users and send them the emails.

Advantages

✅ Can write your scheduling scripts and it doesn’t give load on your actual node server.
✅ Can import node packages & dependencies.
✅ High Performance.
✅ Can provide cron commands to schedule as per your needs.

Related read: How To Make Registration And Login Form using Node.js and MongoDB

coma

Conclusion

In conclusion, this tutorial provided insights into MongoDB Atlas scheduled triggers, guiding you through the setup process and demonstrating how to send emails to users using the SendGrid library. By extracting user data from the database, we explored the potential of scheduled triggers for automating tasks.

Throughout this journey, you gained valuable knowledge on configuring triggers, adding dependencies, and implementing scheduled scripts for enhanced performance.

As you embark on your development endeavours, remember the advantages of Atlas scheduled triggers, including reduced load on your node server, the ability to import dependencies, high performance, and the flexibility of providing cron commands tailored to your needs. Cheers to your newfound expertise!

Keep Reading

Keep Reading

Leave your competitors behind! Become an EPIC integration pro, and boost your team's efficiency.

Register Here
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?