How to execute a js file in MongoDB

22 December,2020 by Jack Vamvas

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")

 

Read more MongoDB commands

Quick Access To Mongodb Commands Cheat Sheet


Author: Jack Vamvas (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

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

Working...
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.

Working...

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


dba-ninja.com