Last active
August 23, 2018 17:35
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("let's try 'listDatabases'"); | |
dblist = JSON.stringify( db.adminCommand('listDatabases'), undefined, 2); | |
// stringify options 2 & 3 add pretty-like spaces | |
print(dblist) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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