Menu Close

MongoDB scripts

These commands may seem somewhat lowbrow compared to the Oracle scripts, but then I have decidedly less experience of MongoDB.

Rotating a log file.

# Connected to the admin database
db.adminCommand( { logRotate : 1 } )

Output the size of every collection on every shard in CSV format.  “tee” should be used to capture the output although it needs editing before importing into something like Excel.

db.getMongo().getDBNames().forEach(function (d) 
{ 
        var this_db = db.getSiblingDB(d); 
        this_db.getCollectionNames().forEach(function(coll) 
        { 
                var c = this_db.getCollection(coll); 
                if ( typeof c != "function") 
                { 
                        var collstats = c.stats();
                        if ( typeof(collstats["shards"]) === 'undefined' )
                        {
                                print(d + "," + coll + ",," + collstats.size + "," + collstats.storageSize);
                        }
                        else
                        {
                                for ( shard_name in collstats["shards"])
                                {
                                        print(d + "," + coll + "," + shard_name + "," + collstats["shards"][shard_name].size + "," + collstats["shards"][shard_name].storageSize);
                                }
                        }
                }       
        })
});

Output the size of a single collection on every shard in CSV format.

use <database>;
var collstats = db.<collection>.stats()
for (var shard_name in collstats["shards"]) {
        print(shard_name + "," + collstats["shards"][shard_name].size + "," + collstats["shards"][shard_name].storageSize);
}

Size of a capped collection

db.<collection>.stats().maxSize
e.g.
db.oplog.rs.stats().maxSize

Collections generating the largest amount of oplog.  This should be run on a secondary to avoid any performance impact.  It can take some time to run depending on the size of the oplog.  When I ran this on a 144GB oplog it took 40 minutes which gives a rate of about 60MB/sec.  I am sure there is some JavaScript that will sort the output, but being somewhat lazy I just ran it through “sort -k 3n”

rs.slaveOk();
use local;
nsMap = {}
db.oplog.rs.find().forEach(function(x) {
    if ( isNaN(nsMap[x.ns]) )
    {
        nsMap[x.ns] = Object.bsonsize(x);
    }
    else
    {
        nsMap[x.ns] += Object.bsonsize(x);
    }
}
);
nsMap

Find the file associated with a collection:

db.<collection>.stats().wiredTiger.uri;

Find which collection is associated with a file.  You can’t do this easily, so this is a brute force approach:

db.adminCommand({listDatabases: 1}).databases.forEach(function listFiles(DBDoc) {
      db.getSiblingDB(DBDoc.name).getCollectionNames().forEach(function listDBFiles(collName) {
            print(DBDoc.name + " " + collName + ": " + db.getSiblingDB(DBDoc.name).getCollection(collName).stats().wiredTiger.uri);
      })
});