Skip to content

Instantly share code, notes, and snippets.

@chilismaug
Last active August 23, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilismaug/34639cd88682014cfe0c4777d3e4a993 to your computer and use it in GitHub Desktop.
Save chilismaug/34639cd88682014cfe0c4777d3e4a993 to your computer and use it in GitHub Desktop.
Common first experiment in mongo shell is saying "show dbs" to see list of databases on the current server. But how to do in a mongo js shell script?
print("let's try 'listDatabases'");
dblist = JSON.stringify( db.adminCommand('listDatabases'), undefined, 2);
// stringify options 2 & 3 add pretty-like spaces
print(dblist) ;
@chilismaug
Copy link
Author

chilismaug commented Aug 23, 2018

When I was going through the basics of the shell in O'Reilly's "MongoDB - The Definitive Guide", there is some discussion of running js shell scripts but the given js equivalent for shell helper "show dbs" would not work in a script, it returns a BSON object that only displays as text while you are in the shell. The command line script output will be "[object BSON]".
This is how to get it to display when running the script from the command line.
to run: mongo mongshl-show-dbs.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment