> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Capped Collections

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 deleting the oldest documents in the collection.

## Restrictions

- Capped collections cannot be sharded.

- Capped collections are not supported in [Stable API](https://www.mongodb.com/docs/manual/reference/stable-api.md#std-label-stable-api) V1.

- You cannot write to capped collections in [transactions.](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions)

- The [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) aggregation pipeline stage cannot write results to a capped collection.

## Command Syntax

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

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

For more information on creating capped collections, see [`createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createCollection.md#mongodb-method-db.createCollection) or [`create`.](https://www.mongodb.com/docs/manual/reference/command/create.md#mongodb-dbcommand-dbcmd.create)

## Use Cases

Generally, [TTL (Time To Live) indexes](https://www.mongodb.com/docs/manual/core/index-ttl.md#std-label-index-feature-ttl) 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.

## Get Started

To create and query capped collections, see these pages:

- [Create a Capped Collection](https://www.mongodb.com/docs/manual/core/capped-collections/create-capped-collection.md#std-label-capped-collections-create)

- [Query a Capped Collection](https://www.mongodb.com/docs/manual/core/capped-collections/query-capped-collection.md#std-label-capped-collections-query)

- [Check if a Collection is Capped](https://www.mongodb.com/docs/manual/core/capped-collections/check-if-collection-is-capped.md#std-label-capped-collections-check)

- [Convert a Collection to Capped](https://www.mongodb.com/docs/manual/core/capped-collections/convert-collection-to-capped.md#std-label-capped-collections-convert)

- [Change the Size of a Capped Collection](https://www.mongodb.com/docs/manual/core/capped-collections/change-size-capped-collection.md#std-label-capped-collections-change-size)

- [Change Maximum Documents in a Capped Collection](https://www.mongodb.com/docs/manual/core/capped-collections/change-max-docs-capped-collection.md#std-label-capped-collections-change-max-docs)

## Behavior

### Oplog Collection

The [oplog.rs](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-oplog) collection that stores a log of the operations in a [replica set](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-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`.](https://www.mongodb.com/docs/manual/reference/command/replSetGetStatus.md#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime)

**Note:**

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

### \_id Index

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

### Updates

Avoid updating data in a capped collection. Updates can expand your data beyond the collection's allocated space and cause unexpected behavior.

### Query Efficiency

Use [natural ordering](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-natural-order) to retrieve the most recently inserted elements from the collection efficiently. This is similar to using the `tail` command on a log file.

### Tailable Cursor

You can use a [tailable cursor](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-tailable-cursor) with capped collections. Similar to the Unix `tail -f` command, a tailable cursor continuously retrieves new documents from the end of a capped collection as they are inserted.

For information on creating a tailable cursor, see [Tailable Cursors.](https://www.mongodb.com/docs/manual/core/tailable-cursors.md#std-label-tailable-cursors-landing-page)

### Multiple Concurrent Writes

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

### Read Concern Snapshot

Starting in MongoDB 8.0, you can use read concern [`"snapshot"`](https://www.mongodb.com/docs/manual/reference/read-concern-snapshot.md#mongodb-readconcern-readconcern.-snapshot-) on [capped](https://www.mongodb.com/docs/manual/core/capped-collections.md#std-label-manual-capped-collection) collections.

## Learn More

- [TTL Indexes](https://www.mongodb.com/docs/manual/core/index-ttl.md#std-label-index-feature-ttl)

- [Index Properties](https://www.mongodb.com/docs/manual/core/indexes/index-properties.md#std-label-index-properties)

- [Indexing Strategies](https://www.mongodb.com/docs/manual/applications/indexes.md#std-label-indexing-strategies)
