Specify which database to use in mongodb .js script

mongo <name of db> --eval "db.runCommand( <js in here> );"

Or if you dont have a specific runCommand script, you can just do:

mongo <name of db> --eval "<js in here>;"

If you are using a return value:

mongo <name of db> --eval "db.eval('return fnName()')"

For a file

mongo <name of db> some_instructions.js

You can use the db.getSiblingDB() method, as mentioned here and in the MongoDB documentation.

// Equivalent for "use <db>" command in mongo shell
db = db.getSiblingDB('foo')

This is particularly useful when writing scripts using the mongo shell where the use helper is not available. Or when you cannot refer to the DB on the connection string.