Docs Menu
Docs Home
/
MongoDB Manual
/ /

Capped Collections

On this page

  • Restrictions
  • Command Syntax
  • Use Cases
  • Get Started
  • Behavior
  • Oplog Collection
  • _id Index
  • Updates
  • Query Efficiency
  • Tailable Cursor
  • Multiple Concurrent Writes
  • Read Concern Snapshot
  • Learn More

Capped collections are fixed-size collections that insert and retrieve documents based on insertion order. Capped collections work similarly to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

The following example creates a capped collection called log with a maximum size of 100,000 bytes.

db.createCollection( "log", { capped: true, size: 100000 } )

For more information on creating capped collections, see createCollection() or create.

Generally, TTL (Time To Live) indexes offer better performance and more flexibility than capped collections. TTL indexes expire and remove data from normal collections based on the value of a date-typed field and a TTL value for the index.

Capped collections serialize write operations and therefore have worse concurrent insert, update, and delete performance than non-capped collections. Before you create a capped collection, consider if you can use a TTL index instead.

The most common use case for a capped collection is to store log information. When the capped collection reaches its maximum size, old log entries are automatically overwritten with new entries.

To create and query capped collections, see these pages:

Consider these behavioral details for capped collections.

The oplog.rs collection that stores a log of the operations in a replica set uses a capped collection.

Unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the majority commit point.

Note

MongoDB rounds the capped size of the oplog up to the nearest integer multiple of 256, in bytes.

Capped collections have an _id field and an index on the _id field by default.

Avoid updating data in a capped collection. Because capped collections are fixed-size, updates can cause your data to expand beyond the collection's allocated space, which can cause unexpected behavior.

Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. This is similar to using the tail command on a log file.

You can use a tailable cursor with capped collections. Similar to the Unix tail -f command, the tailable cursor "tails" the end of a capped collection. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.

For information on creating a tailable cursor, see Tailable Cursors.

If there are concurrent writers to a capped collection, MongoDB does not guarantee that documents are returned in insertion order.

Starting in MongoDB 8.0, you can use read concern "snapshot" on capped collections.

Back

On-Demand Materialized Views