1

How can I set an event scheduler in Mongodb?

I will create an event that every 4 hours calculate fields (SUM|COUNT) in the collection then save it.

What is the Best Solution to arrive at this? thanks:)

Mahmoud.Eskandari
  • 1,460
  • 3
  • 20
  • 32
  • 1
    This seems like a good fit for my package called [mongodb-cron](https://www.npmjs.com/package/mongodb-cron). – xpepermint Jun 17 '17 at 12:06
  • 1
    You can emulate a cronjob with MongoDB 3.6, using Change Streams and TTL - code here: https://stackoverflow.com/questions/49389459/mongodb-is-it-possible-to-capture-ttl-events-with-change-stream-to-emulate-a-sc – João Otero Mar 22 '18 at 13:31

2 Answers2

4

As of version 3.2, MongoDB has no built-in cronjob system (with the exception of the TTL index auto-delete jobs, but these won't help you to solve this problem).

That means you will have to use an external tool to start the mongo shell with your query as a parameter to it. There are countless tools available for this. Some build into all common operating systems out-of-the box and some in form of 3rd party tools. There are far too many options to list; which is the best depends on your IT infrastructure and what your system administrators prefer.

Baris Senyerli
  • 642
  • 7
  • 13
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • It's Very Bad:( because out-of-the box Applications maybe no be Stable! – Mahmoud.Eskandari Mar 29 '16 at 18:06
  • @M.Eskandari The OS scheduling systems are very reliable indeed. They've been around a *long* time and have been well proven. – BanksySan Mar 29 '16 at 18:14
  • @M.Eskandari Scheduling cronjobs is a topic for system administrators to worry about. We are software developers, so this is not our area of expertise. If you would like to talk to system administrators, try https://serverfault.com – Philipp Mar 29 '16 at 18:39
  • Dear @Philipp , Events is part of database's , in mysql I write Events `CREATE DEFINER=`root`@`localhost` EVENT...` like it and I think Stackoverflow is better place for this Question! I known Serverfault too, This Comment is better for inside my Question not this Answer :)))) ,Thanks – Mahmoud.Eskandari Mar 29 '16 at 22:38
  • @M.Eskandari Events might be part of MySQL and some other relational databases, but they are certainly not a part of MongoDB. – Philipp Mar 29 '16 at 22:41
  • I made a light NPM package https://github.com/xpepermint/mongodb-cron which can help. I've been using it in a clustered environment for a long time and I'm very happy with the results. – xpepermint May 09 '17 at 12:29
1

Try MongoDB Cron package. It's a simple API for scheduling tasks and running recurring jobs on MongoDB collections. Any collection can be converted into a job queue or crontab list.

xpepermint
  • 35,055
  • 30
  • 109
  • 163