w3resource
MongoDB Tutorial

MongoDB: db.collection.stats() method

db.collection.stats

The db.collection.stats() method is used to return statistics about the collection.

Syntax:

db.collection.stats(scale | options)

Parameters:

Name Description Required /
Optional
Type
scale The scale used in the output to display the sizes of items. By default, output displays size in bytes. To display kilobytes rather than bytes, specify a scale value of 1024. Optional number
options Alternative to scale parameter. Use the options document to specify options, including scale. Optional document

The options document can contain the following fields and values:

Name Description Required /
Optional
Type
scale Optional. The scale used in the output to display the sizes of items. By default, output displays size in bytes. To display kilobytes rather than bytes, specify a scale value of 1024. Optional number
indexDetails If true, db.collection.stats() returns index details in addition to the collection stats.
Only works for WiredTiger storage engine.
Defaults to false.
Optional boolean
indexDetailsKey If indexDetails is true, you can use indexDetailsKey to filter index details by specifying the index key specification. Only the index that exactly matches indexDetailsKey will be returned.
If no match is found, indexDetails will display statistics for all indexes.
Use getIndexes() to discover index keys. You cannot use indexDetailsKey with indexDetailsName.
Optional document
indexDetailsName If indexDetails is true, you can use indexDetailsName to filter index details by specifying the index name. Only the index name that exactly matches indexDetailsName will be returned.
If no match is found, indexDetails will display statistics for all indexes.
Use getIndexes() to discover index names. You cannot use indexDetailsName with indexDetailsField.
Optional string

Returns:

A document that contains statistics on the specified collection.

Sample document in the restaurants collection:

{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}

Example: Basic Stats Lookup

The following operation returns stats on therestaurantscollection:

db.restaurants.stats()

Output:

> db.restaurants.stats();
{
        "ns" : "test.restaurants",
        "count" : 25359,
        "size" : 14721552,
        "avgObjSize" : 580,
        "storageSize" : 22507520,
        "numExtents" : 7,
        "nindexes" : 1,
        "lastExtentSize" : 11325440,
        "paddingFactor" : 1,
        "systemFlags" : 0,
        "userFlags" : 1,
        "totalIndexSize" : 752192,
        "indexSizes" : {
                "_id_" : 752192
        },
        "ok" : 1
}

Retrieve the restaurants data from here

Previous: db.collection.save() method
Next: db.collection.storageSize() method



New Content: Composer: Dependency manager for PHP, R Programming