Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument to Mongo Script

I've been using mongo and script files like this:

$ mongo getSimilar.js 

I would like to pass an argument to the file:

$ mongo getSimilar.js apples 

And then in the script file pick up the argument passed in.

var arg  = $1; print(arg); 
like image 741
Brig Avatar asked Apr 11 '12 21:04

Brig


1 Answers

Use --eval and use shell scripting to modify the command passed in.

mongo --eval "print('apples');"

Or make global variables (credit to Tad Marshall):

$ cat addthem.js printjson( param1 + param2 ); $ ./mongo --nodb --quiet --eval "var param1=7, param2=8" addthem.js 15 
like image 159
jcollum Avatar answered Oct 08 '22 09:10

jcollum