How to execute a js file in MongoDB

Question: I have a js file – which iterates through some MongoDB collections – and currently I execute on an adhoc basis using the mongo shell.    I now need to schedule this process on a regular basis and researching some effective methods to use? Can you supply some examples methods to  deploy 

Answer:  I use  two main methods to execute external js files . The first method utilises mongo shell   & the second method use the MongoDB load function. Using this method gives you flexibility to trigger steps with a scheduler such as Crontab

Method 1 : Using the Mongo shell

mongo localhost:27017/mytest myjsfile.js

Method 2: Using the MongoDB load function

The load function supports absolute and relative file paths.

load(“myjsfile.js”)

–or

load(“/mypath/myjsfile.js”)

 

Author: Jack Vamvas

Leave a Reply

Discover more from ciquery

Subscribe now to keep reading and get access to the full archive.

Continue reading