Skip to content

Instantly share code, notes, and snippets.

@unicornist
Last active November 16, 2020 01:01
Show Gist options
  • Save unicornist/c10cb86c6113aba7fb31c1e092c18552 to your computer and use it in GitHub Desktop.
Save unicornist/c10cb86c6113aba7fb31c1e092c18552 to your computer and use it in GitHub Desktop.
Get all MongoDB database names and collections names for each, using mongo binary and jq
dbs=$(
mongo --quiet --eval "db.adminCommand('listDatabases')" |
jq ".databases[].name|select((.!=\"admin\") and (.!=\"config\") and (.!=\"local\"))" |
sed "s/\"//g"
)
for db in $dbs
do
collections=$(
mongo $db --quiet --eval 'db.getCollectionNames()' |
jq .[] |
sed "s/\"//g"
)
for collection in $collections
do
echo $db - $collection
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment