Monitoring Tooling / Performance Analysis with Collection Stats

Code Recap: Performance Analysis with Collection Stats

The db.collection_name.stats() command retrieves statistics specific to a collection, such as the document count, storage size, and index metrics. It helps administrators evaluate the efficiency and growth of individual collections.

db.collection_name.stats()

The output for db.collection_name.stats() is quite comprehensive. To narrow down the output to a specific category of information by appending that category name to the command. This command narrows the output to just the storage space allocated for this collection, which can help track resource utilization trends. This provides valuable data for capacity planning and collection performance analysis.

db.collection_name.stats().storageSize
118197253

Gathering Statistics About Index Usage

This command is used to gather statistics about indexes within the products collection, including the number of operations that utilized each index and the timestamp of the last operation.

db.products.aggregate([ { $indexStats: {} }] );
[
  . . .
  {
    name: 'ratings_1',
    key: { ratings: 1 },
    host: 'atlas-zr1do5-shard-00-02.xwgj1.mongodb.net:27017',
    accesses: { ops: Long('0'), since: ISODate('2025-05-01T20:08:40.393Z') },
    spec: { v: 2, key: { ratings: 1 }, name: 'ratings_1' }
  }
]

Commands for Hiding and Dropping Indexes

The hideIndex() command disables the ratings_1 index temporarily. The query planner ignores the index during execution, but it remains stored in the metadata and can be re-enabled later.

db.products.hideIndex('ratings_1')

The dropIndex command permanently removes the ratings_1 index, reducing storage and maintenance overhead. Dropping unused indexes can optimize storage and enhance performance when irrelevant indexes are eliminated.

db.products.dropIndex('ratings_1')