Created
March 3, 2020 06:13
-
-
Save izmailoff/7e81628501155d45fcc0a9d1aea331b4 to your computer and use it in GitHub Desktop.
Count MongoDB documents in all collections
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
#!/bin/bash | |
db_name="$1" | |
if [ -z "$db_name" ]; then | |
echo "Usage: $0 <db_name or uri>" | |
exit 1 | |
else | |
echo "Collections in db: $db_name." | |
mongo --quiet "$db_name" <<EOF | |
var collections = db.getCollectionNames(); | |
for(var i = 0; i < collections.length; i++) { | |
var name = collections[i]; | |
if(name.substr(0, 6) != 'system') | |
print(name + ': ' + db[name].count() + ' documents'); | |
} | |
quit(); | |
EOF | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment