How to create schedule trigger in MongoDB Atlas
Tags: javascript, mongodb, webdev, tutorial
Published: 2022-01-06 08:55:28

`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41rqbz95vdh2na1x4pze.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj2veb9o1k3yy3pqj15u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0agsoeg5lkeuwccl0csq.png)

![script panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qyqhcp07arshb4kaxht.png)

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

![Realm Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/226ftf1g4tiimvu2pyib.png)

![create RealM app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/037nlvbejdezkzucxsoa.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7ep4meck1tl73hg0p1u.png)

![My daily order report schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc1nnipi2yu0e3szr7r0.png)

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](https://www.youtube.com/channel/UCkaoWtBz6ioLbNwRzIco5Yg)
[GitHub](https://github.com/zainbinfurqan)
[Twitter](https://twitter.com/zainahmed241995)
[LinkedIn](https://www.linkedin.com/in/zain-ahmed-231349132/)