getMongoData.js is a utility for gathering information about how a running
MongoDB deployment has been configured and for gathering statistics about its
databases, collections, indexes, and shards.
For sample output, see getMongoData.log.
To execute on a locally-running mongod or mongos on the default port (27017)
without authentication, run:
mongo --quiet --norc getMongoData.js > getMongoData-output.json
To execute on a remote mongod or mongos with authentication (see the next
section for the minimum required permissions), run:
mongo HOST:PORT/admin -u ADMIN_USER -p ADMIN_PASSWORD --quiet --norc getMongoData.js > getMongoData-output.json
If ADMIN_PASSWORD is omitted, the shell will prompt for the password.
To have the output be in a more human-readable (non-JSON format), modify the above
commands to include the following --eval option, as demonstrated for the local
execution:
mongo --eval "var _printJSON=false;" getMongoData.js > getMongoData-output.log
To have a mongos for a sharded cluster output full details of chunk
distribution across shards, include var _printChunkDetails=true in the
--eval option:
mongo --quiet --norc --eval "var _printChunkDetails=true; var _ref = 'Support Case NNNNN'" getMongoData.js > getMongoData-output.json
getMongoData.js is JavaScript script which must be run using the mongo shell
against either a mongod or a mongos.
Minimum required permissions (see MongoDB Built-In Roles):
- A database user with the
backup,readAnyDatabase, andclusterMonitorroles. These are essentially read-only roles except the backup role allows writes to two MongoDB system collections -admin.mms.backupandconfig.settings. Thebackuprole is necessary in order for the script to output the number of database users and user-defined roles configured. - A root/admin database user may be used as well.
Example command for creating a database user with the minimum required permissions:
db.getSiblingDB("admin").createUser({
user: "ADMIN_USER",
pwd: "ADMIN_PASSWORD",
roles: [ "backup", "readAnyDatabase", "clusterMonitor" ]
})If you would like to have truely read-only permissions, a custom role needs be created.
// getMongoData.js <=4.1.0
db.getSiblingDB("admin").createRole({
role: "gmd",
privileges: [
{resource: {db: "", collection: "system.users"}, actions: ["collStats", "listIndexes", "find"]},
{resource: {db: "", collection: "system.roles"}, actions: ["collStats", "listIndexes", "find"]},
{resource: {db: "", collection: "system.version"}, actions: ["collStats", "listIndexes", "find"]},
{resource: {db: "", collection: "system.profile"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "", collection: "system.js"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "", collection: "system.views"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "", collection: "system.indexBuilds"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "", collection: "system.preimages"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "", collection: "system.buckets"}, actions: ["collStats", "listIndexes"]},
{resource: {db: "local", collection: ""}, actions: ["collStats", "listCollections", "listIndexes"]},
{resource: {db: "config", collection: ""}, actions: ["collStats", "listCollections", "listIndexes"]},
],
roles: ["readAnyDatabase", "clusterMonitor"]
});
// getMongoData.js >=4.1.1
db.getSiblingDB("admin").createRole({
role: "gmd",
privileges: [
{resource: {db: "", collection: "system.users"}, actions: ["find"]},
{resource: {db: "", collection: "system.roles"}, actions: ["find"]}
],
roles: ["readAnyDatabase", "clusterMonitor"]
});
db.getSiblingDB("admin").createUser({
user: "ADMIN_USER",
pwd: "ADMIN_PASSWORD",
roles: [ "gmd" ]
})The most notable methods, commands, and aggregations that this script runs are listed below.
Server Process Config & Stats
Replica Set Config & Stats
Database Users and User-Defined Roles (the count only)
- db.system.users.count() in the "admin" database
- db.system.roles.count() in the "admin" databases
Database, Collection, and Index Config & Stats
- listDatabases
- db.getCollectionNames()
- db.stats()
- db.getProfilingStatus()
- db.collection.stats()
- db.collection.getShardDistribution()
- db.collection.getIndexes()
- $indexStats
Sharding Config & Stats
- Queries and aggregations on various collections in the MongoDB config database, including the "version", "settings", "routers", "shards", "databases", "chunks", and "tags" collections.
Queryable Encryption (QE) Config
- db.getCollectionInfos()
- Performs queries and aggregations on QE collections and auxiliary collections in all databases. The output is an array of objects, each containing information about a queryable encrypted collection.
- This script should take on the order of seconds to run.
- If your deployment has more than 2500 collections, this script will by default fail.
Please note: all tools/ scripts in this repo are released for use "AS IS" without any warranties of any kind, including, but not limited to their installation, use, or performance. We disclaim any and all warranties, either express or implied, including but not limited to any warranty of noninfringement, merchantability, and/ or fitness for a particular purpose. We do not warrant that the technology will meet your requirements, that the operation thereof will be uninterrupted or error-free, or that any errors will be corrected.
Any use of these scripts and tools is at your own risk. There is no guarantee that they have been through thorough testing in a comparable environment and we are not responsible for any damage or data loss incurred with their use.
You are responsible for reviewing and testing any scripts you run thoroughly before use in any non-testing environment.
Thanks,
The MongoDB Support Team