DEV Community

Zain Ahmed
Zain Ahmed

Posted on

How to create schedule trigger in MongoDB Atlas

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, weak and month etc.

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

First I will define a scenario which I will implement on trigger script.

let assume you run a e-commerce store and you want to get a daily report of orders you get on a single day this report generate on daily basis and save on database so you can easily see it on just one click. Here are tables involve in this logic “Orders” and “DailyOrderReport”.

Steps

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

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

Trigger list

Step 3: You need to select some options

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

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

Trigger configuration

Step 6: Select the Event type as function this will open panel for write script

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

Trigger script

script panel

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

Realm Dashboard

create RealM app

Now one more thing we need to discuss here is on script you need to get the instance of you cluster “context.services.get(“web-e-commerce”)” this one and for that you need to create a application RealM, if you see on your dashboard just search for the keyword RealM and you once you find it click on it and it will navigate you to the realm dashboard where you need to create a application and define a name for that with select the cluster from dropdown. Now after all the just copy the application name you define on realm and click save then past this name in script line like “ 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 filed name “Execution time” this field in trigger list for every trigger will tell you at what time you trigger runs now if you can wait 2 min after that go to you collection “DailyOrderReport you can see the entry on it, and that’s it. you did a good job.

My orders schema

My daily order report schema

If you like my content you can also check out my content on My YouTube channel Link is down blew, More you can support me by Like & Share this content so more can get benefit from this.

My Youtube Channel YarCoder
GitHub
Twitter
LinkedIn

Discussion (0)