liveBook · Manning

Capped collection

As explained in MongoDB 8.0 in Action, Third Edition

A capped collection is a fixed-size MongoDB collection that preserves insertion order and limits stored data by size or count.

A capped collection is a fixed-size MongoDB collection designed to keep recent documents. It can also have a document-count limit. Like a circular buffer, it makes room at its size limit by replacing the oldest documents with new ones. section 2.6.3

Capped collections preserve insertion order. Documents can be retrieved in that order without an index for it, reducing indexing overhead and supporting high-throughput inserts and retrievals. section 2.6.3

Use one when retaining the newest data matters more than retaining every record:

  • High-volume logs: first-in-first-out retention keeps events in sequence while bounding storage; writes without indexes can be nearly as fast as filesystem logging. section 2.6.3
  • Small caches: recent entries are often the most useful, though necessary indexes may add some overhead; keeping a read-heavy cache in RAM is preferable where possible. section 2.6.3

Choose a capped collection only if automatic replacement is acceptable: reaching the limit removes older data. If those documents must remain available, that behavior is a poor fit. For automatically expiring data, MongoDB also offers time-to-live indexes; for timestamped metrics or device data, it offers time-series collections. section 2.6.3 section 1.3.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines fixed-size, circular-buffer behavior and explains performance and retention trade-offs, without showing capped-collection creation commands.
Examples
Illustrates use with high-volume system logs and small, read-heavy caches.
What it assumes
Assumes basic familiarity with MongoDB collections and documents while defining capped behavior directly.
What you can do after
Evaluate/understand whether bounded, first-in-first-out retention fits a logging or caching need.
How it connects
Relates capped collections to circular buffers, indexes, time-to-live indexes, and time-series collections.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.