Skip to content

Instantly share code, notes, and snippets.

@izmailoff
Created March 3, 2020 06:13
Show Gist options
  • Save izmailoff/7e81628501155d45fcc0a9d1aea331b4 to your computer and use it in GitHub Desktop.
Save izmailoff/7e81628501155d45fcc0a9d1aea331b4 to your computer and use it in GitHub Desktop.
Count MongoDB documents in all collections
#!/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