Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links



How to execute a js file in MongoDB

22 December,2020 by Rambler

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.





Read more MongoDB commands

Quick Access To Mongodb Commands Cheat Sheet

Author: Rambler (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on How to execute a js file in MongoDB