What does the max field mean in the output of MongoDB db..stats( )?

The max field in MongoDB's db.<collectionname>.stats() output represents the maximum number of documents allowed in a capped collection. This field only appears for capped collections and shows the document limit set during collection creation.

Syntax

db.createCollection("collectionName", { 
    capped: true, 
    size: sizeInBytes, 
    max: maxDocuments 
});

Create Sample Capped Collection

Let's create a capped collection with a maximum of 50 documents ?

db.createCollection("demo673", { 
    capped: true, 
    size: 100, 
    max: 50 
});
{ "ok": 1 }

Insert Sample Documents

db.demo673.insertMany([
    { Name: "John", Age: 23 },
    { Name: "Bob", Age: 21 },
    { Name: "David", Age: 20 }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5ea3ec7304263e90dac943e8"),
        ObjectId("5ea3ec7804263e90dac943e9"),
        ObjectId("5ea3ec7f04263e90dac943ea")
    ]
}

View Collection Statistics

Check the stats to see the max field value ?

db.demo673.stats();
{
    "ns": "test.demo673",
    "capped": true,
    "max": 50,
    "maxSize": 100,
    "count": 3,
    ...
}

Verify Documents

db.demo673.find();
{ "_id": ObjectId("5ea3ec7304263e90dac943e8"), "Name": "John", "Age": 23 }
{ "_id": ObjectId("5ea3ec7804263e90dac943e9"), "Name": "Bob", "Age": 21 }
{ "_id": ObjectId("5ea3ec7f04263e90dac943ea"), "Name": "David", "Age": 20 }

Key Points

  • The max field only appears in stats for capped collections
  • Once the document limit is reached, MongoDB removes the oldest documents automatically
  • Both size and max constraints are enforced − whichever limit is hit first triggers document removal

Conclusion

The max field in collection stats indicates the maximum document limit for capped collections. It helps enforce automatic document rotation when storage or count limits are exceeded.

Updated on: 2026-03-15T03:27:59+05:30

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements