What is capped collection in MongoDB

Definition:MongoDB Capped collection is a fixed-sized collection that automatically overwrites its oldest entries when it reaches its maximum size.

What is capped collection in MongoDB
In detailed : Capped collections have maximum size or document counts that prevent them from growing beyond maximum thresholds. All capped collections must specify a maximum size and may also specify a maximum document count. MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

How to create Capped collection:


> use r2schools switched to db r2schools > db.createCollection("empcount", { capped : true, size : 5242880, max : 5000 } ) { "ok" : 1 }

In the above example, if we put capped: false then it will be non-capped collection.

How to verify whether a collection is capped or not

> use r2schools
switched to db r2schools
> db.empcount.isCapped()
true
> db.emp.isCapped()
false

If the output is true, means collection is capped and otherwise collection non-capped collection. So, in the above queries, empcount is capped collection and emp is non-capped collection.