What are Capped Collections? — Interview Answer — MongoDB Technical Interview Questions & Answers

Model answer covering fixed size and insertion order.

What are Capped Collections? — Interview Answer

  • Model answer: Capped collections have a max size and overwrite the oldest documents. Used for logs. Created with createCollection options.
  • Not for data you must keep forever.

Technical Interview Tip: Ring buffer. One use case.

Code Examples for What are Capped Collections? — Interview Answer

What are Capped Collections? — Interview Answer — working example

db.createCollection("logs", { capped: true, size: 1048576, max: 5000 })
db.logs.insertOne({ level: "info", msg: "app started", at: new Date() })
db.logs.find().sort({ $natural: -1 }).limit(5)

What are Capped Collections? — Interview Answer — capped collections are fixed-size and overwrite oldest documents in insertion order.

How to Explain What are Capped Collections? — Interview Answer to an Interviewer

Capped collections?

Fixed size Oldest documents dropped when full