How to Create Schedule Trigger in MongoDB Atlas

Learn this brilliant method to set your script to run at specific time intervals in MongoDB Atlas.

Zain Ahmed
4 min readJan 5, 2022

Do you know that you can create similar like cron job in MongoDB Atlas where you can set a script to run on specific time, it can be run every hour, week, or month, etc.

Let see how we can configure the Schedule trigger on MongoDB atlas.

First I will define a scenario that I will implement on the trigger script.

Let’s assume you run an e-commerce store and you want to get a daily report of orders you get on a single day, this report generates on daily basis and is saved on a database so you can easily see it with just one click. Here are the tables involved in this logic — “Orders” and “DailyOrderReport”.

Step 1: Go to MongoDB Atlas user dashboard and on the left side, you can see a Trigger option. Go to this option.

Step 2: On trigger, click on create a trigger and this action will navigate you to the new trigger configuration page.

Step 3: You need to select some options.

Step 4: Click on the Schedule option then define a name for the trigger, Enable trigger, and select schedule type basic.

Step 5: Select the job repetition time like I select to run this trigger every 2 mins.

Step 6: Select the Event type as function this will open a panel for writing script.

Step 7: On the panel, you can write the script to run after every defined time, You can also test the script through this panel, and if all is good you can save the trigger and it will be listed on the trigger list.

Here is my script which I write for my logic to get dailyOrderReport and save it in a new schema.

Now one more thing we need to discuss here is that in the script you need to get the instance of your cluster, i.e., “context.services.get(“web-e-commerce”)” and for that, you need to create an application called RealM. If you don't see it on your dashboard just search for the keyword RealM and once you find it, click on it and it will navigate you to the RealM dashboard. Here you need to create an application and define a name for that with select the cluster from the dropdown. Now, after all that, just copy the application name you define on RealM and click save then paste this name in a script line like this: “context.services.get(“web-e-commerce”)”.

Now that you are all set to go, just save the trigger and move back to the trigger list, there is a file named “Execution time” this field in the trigger list for every trigger will tell you at what time your trigger runs. Now, if you can wait 2 mins after that go to your collection: “DailyOrderReport you can see the entry on it, and that’s it, you did a good job.

If you like my content you can also check out my content on my YouTube channel (link given below). You can also support me by Liking & Sharing this content so more can benefit from this information.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--

Zain Ahmed

Hi I am Zain Ahmed Software Engineer with passionate about try different technologies and try some cool staff, I create Blogs/Content on different platform.