Monitoring Tooling / Performance Analysis with db.serverStatus and db.stats

Code Recap: Performance Analysis with db.serverStatus and db.stats

Using the db.serverStatus() Command

The db.serverStatus() command retrieves comprehensive diagnostic information about the current state of MongoDB. It includes metrics such as operation rates, server connections, memory usage, storage engine statistics, and replica set info. It is often used by administrators to monitor server health and performance.

db.serverStatus()

The output for db.serverStatus() is quite comprehensive. To narrow down the output to a specific category of information by appending that category name to the command.

db.serverStatus().<category_name>

The following command outputs the opcounters metric which shows counts of various commands executed, such as insert, query, update, delete, getmore, and command. These numbers reflect the workload processed by the server since its startup.

db.serverStatus().opcounters
{
  insert: Long('138'),
  query: Long('28459375'),
  update: Long('78190'),
  delete: Long('37055'),
  getmore: Long('682296'),
  command: Long('8884086')
}

The following command outputs the locks metric which gives details about lock acquisition at various levels (global, database, and collection). The timeAcquiringMicros field implies how much time locks are held, which can help assess contention. This detailed output is useful for debugging slow operations or determining server utilization.

db.serverStatus().locks
{
  MultiDocumentTransactionsBarrier: { acquireCount: { W: Long('6') } },
  ReplicationStateTransition: {
    acquireCount: { w: Long('13970184'), W: Long('2') },
    acquireWaitCount: { w: Long('1'), W: Long('1') },
    timeAcquiringMicros: { w: Long('10545'), W: Long('1') }
  },
  Global: {
    acquireCount: { r: Long('50324542'), w: Long('6216098'), W: Long('6') }
  },
  Database: {
    acquireCount: { r: Long('20982'), w: Long('5619534'), W: Long('1') }
  },
  Collection: {
    acquireCount: {
      r: Long('30556'),
      w: Long('5619508'),
      R: Long('2'),
      W: Long('14')
    }
  },
  Mutex: { acquireCount: { r: Long('20') } },
  oplog: { acquireCount: { r: Long('456'), w: Long('1') } }
}

Using the db.stats() Method

The db.stats() method provides statistics about the database as a whole, including details such as the number of collections, views, the size of stored data, and the amount of allocated memory. It aids database administrators in evaluating resource usage and storage efficiency.

db.stats()
{
    db: 'daintree',
    collections: Long('3'),
    views: Long('0'),
    objects: Long('2199'),
    avgObjSize: 1247.5202364711233,
    dataSize: 2743297,
    storageSize: 1249280,
    indexes: Long('4'),
    indexSize: 729088,
    totalSize: 1978368,
    scaleFactor: Long('1'),
    fsUsedSize: 2085855232,
    fsTotalSize: 10632560640,
    ok: 1,
    '$clusterTime': {
        clusterTime: Timestamp({ t: 1746125325, i: 1 }),
        signature: {
            hash: Binary.createFromBase64('SEfIGPSZ4wh4O9Au9C4GUSH0z90=', 0),
            keyId: Long('7475100002675589125')
        }
    },
    operationTime: Timestamp({ t: 1746125325, i: 1 })
}