MongoDB Documentation, Release 3.0.4 • You cannot use any shell helper (e.g. use <dbname> , show dbs , etc.) inside the JavaScript file because they are not valid JavaScript. The following table maps the most common mongo shell helpers to their JavaScript equivalents. To print all items in a result cursor in mongo shell scripts, use the following idiom: From the system prompt, use mongo to evaluate JavaScript. --eval option Use the --eval option to mongo to pass the shell a JavaScript fragment, as in the following: mongo test --eval "printjson(db.getCollectionNames())" This returns the output of db.getCollectionNames() using the mongo shell connected to the mongod or mongos instance running on port 27017 on the localhost interface. 271 5.2. Administration Tutorials Scripting • In interactive mode, mongo prints the results of operations including the content of all cursors. In scripts, either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON. cursor = db.collection.find(); while ( cursor.hasNext() ) { printjson( cursor.next() ); } Example Shell Helpers JavaScript Equivalents show dbs , show databases db.adminCommand('listDatabases') use <db> db = db.getSiblingDB('<db>') show collections db.getCollectionNames() show users db.getUsers() show roles db.getRoles({showBuiltinRoles: true }) show log <logname> db.adminCommand({ 'getLog' : '<logname>' }) show logs db.adminCommand({ 'getLog' : ' * ' }) it cursor = db.collection.find() if ( cursor.hasNext() ){ cursor.next(); }