In this blog post, we will discuss the capped collection of MongoDB. Capped collection is a fixed-type collection that inserts docs in a circular fashion. This means once allocated files are full, data at the beginning of the first file is overwritten. Consider this: we define a capped collection of size 1GB, it will purge out the oldest document if the allocated size of the collection is full and we have to insert a new document.

A capped collection guarantees that it will maintain the document’s insertion order. Due to this, it doesn’t require an extra index to retrieve a document. This helps a capped collection to maintain a high throughput insertion. Capped collection’s document contains the _id field, which is by default index, and deletion of the document will happen based on the oldest _id. MongoDB automatically increases the capped collection’s provided size to make it an integer multiple of 256. One should avoid updating a doc in a capped collection, but if you want to update, you can. Only if you don’t increase the original size of the document, and it is recommended to be light on updates as it scans the whole collection. Create an index to avoid collection scan.

A capped collection can be created as below:

We can also specify the maximum number of docs in the capped collection.

Convert a collection to capped collection:

Verify if it’s a capped collection:

Query a capped collection:

MongoDB guarantees that retrieving of the docs will be in the same order as it was inserted. 

To return the docs in reverse insertion order, use the sort method with $natural parameter set to -1.

Change a capped collection size:

From MongoDB v6.0 onwards, capped collection size can be resized. However, before resizing the capped collection, ensure featureCompatibilityVersion is set to at least “6.0”.

If you try to resize the capped collection in a version older than v6.0 (without setting featureCompatibilityVersion to 6.0) then it will fail with the error unknown option to collMod: cappedSize:

Advantage:

  1. Supports high insertion throughput:  Capped collection keeps data in insert order and removes the index overhead; due to this, it supports high insertion throughput.
  2. Capped collection is useful in storing the log information as it keeps the data ordered by the events.

Disadvantage:
There are some restrictions of the capped collection.

  1. A capped collection can’t be sharded.
  2. A capped collection can’t have TTL indexes.

Summary

Capped collection can be useful to store log file information as it’s close to the speed of writing log information directly to a file system without the index overhead. MongoDB itself uses the capped collection for replication of oplog.rs collection’s storage mechanism due to its solid performance. Oplog.rs collection is a special capped collection in which we cannot create an index, insert a document, or drop the collection. Capped collection has advantages and disadvantages, so before using a capped collection, make sure you understand the application’s requirements and decide accordingly.

We also encourage you to try our products for MongoDB, like Percona Server for MongoDB, Percona Backup for MongoDB, and Percona Operator for MongoDB. We also recommend checking out our blog MongoDB: Why Pay for Enterprise When Open Source Has You Covered?

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments