'best way to execute this task on nodejs?

lets suppose i have a collection with 4k documents on mongodb. Every 10 seconds for example i need to loop for each document and make some verifications. Im wondering now what is the besy way I could do that? Do I need multithread or something like that to speed up the proccess?



Solution 1:[1]

fire off your script using cron

your database will need to be able to handle the traffic, so you would have to play with horizontal scaling or vertical scaling if there are issues or look at cache options like redis or Elasticsearch which can handle faster hits, mongoDB will be able to handle it with the correct hardware otherwise.

consider adding a lock while each query is running in case one of them takes too long, IE make a collection for checking if a lock is turned on prior to executing the query script, turn off the lock once the query finishes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alilland