I am attempting to pass parameters from a shell script to a MongoDB JavaScript file.
My current approach is as follows:
mongo --eval "var ADMIN_USER_NAME='$ADMIN_NAME', ADMIN_USER_PASSWORD='$ADMIN_PASSWORD'" insertusers.js
The contents of insertusers.js
are:
cat insertusers.js
use admin
db.addUser( { user: ADMIN_USER_NAME,
pwd: ADMIN_USER_PASSWORD,
roles: [ "userAdminAnyDatabase" ] } )
Unfortunately, when I run the command, I encounter the following error:
mongo --eval "var ADMIN_USER_NAME='$ADMIN_NAME', ADMIN_USER_PASSWORD='$ADMIN_PASSWORD'" insertusers.js
MongoDB shell version: 2.4.9
connecting to: test
Fri Apr 11 21:30:34.791 SyntaxError: Unexpected identifier at insertusers.js:5
failed to load: insertusers.js
I'm puzzled by this failure. The same functionality works when executed in a JavaScript shell.
Upon reaching the line use admin
, an error
SyntaxError: Unexpected identifier
occurs.