How to pass argument to Mongo Script How to pass argument to Mongo Script javascript javascript

How to pass argument to Mongo Script


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.jsprintjson( param1 + param2 );$ ./mongo --nodb --quiet --eval "var param1=7, param2=8" addthem.js15


You can't do that, but you could put them in another script and load that first:

// vars.jsmsg = "apples";

and getSimilar.js was:

print(msg);

Then:

$ mongo vars.js getSimilar.jsMongoDB shell version: blahconnecting to: testloading file: vars.jsloading file: getSimilar.jsapples

Not quite as convenient, though.


Set a shell var:

password='bladiebla'

Create js script:

cat <<EOT > mongo-create-user.jsprint('drop user admin');db.dropUser('admin');db.createUser({user: 'admin',pwd: '${password}',roles: [ 'readWrite']});EOT

Pass script to mongo:

mongo mongo-create-user.js