Capped collections

A fixed-size collection that drops its oldest documents when it fills up.

Create a fixed-size collection that drops its oldest documents when it fills up, useful for logs and queues.

Requirements

  • A user with dbAdmin or readWrite on the database to create collections.

Create

JavaScript
db.createCollection("log", { capped: true, size: 64 * 1024 * 1024, max: 100000 });

Follow the tail

JavaScript
const cursor = db.log.find().tailable({ awaitData: true });

A capped collection takes a range of its own that never splits or merges.

Check

With the collection full, each new insert makes the oldest document disappear, so the count stays at max.

Common problems

  • A capped collection is not spread across nodes, so its traffic goes to a single leader.
  • It cannot also be a clustered collection.

See also