Open In App

Capped Collections in MongoDB

Capped collections are fixed-size collections means when we create the collection, we must fix the maximum size of the collection(in bytes) and the maximum number of documents that it can store. After creation, if we try to add more than documents to their capacity, it overwrites the existing documents. It supports high-throughput operations, that are useful when we insert and retrieve documents based on insertion order. The working of the capped collection is similar to circular buffers, which means once the fixed space is allocated for the capped collection, it creates/makes spaces for the new documents by overwriting the oldest documents in the given collection. Capped collection can also have _id field and by default, each _id field has an index.

How to create a Capped collection?

We create the capped collections using the createCollection() method in MongoDB. When we create a new capped collection, we must specify the maximum size of the collection in bytes, which will MongoDB pre-allocate for the collection. The size of the capped collection contains a small amount of space for the internal overhead and if the value of the size field is less than or equal to 4096, then the capacity of the collection is 4096 bytes. Otherwise, MongoDB will automatically increase the size to make it an integer multiple of 256 and specify a maximum document count using the max field. If the given collection reaches the maximum size limit before it reaches the maximum document count, then MongoDB removes older documents to create space for the new document.



Syntax:

db.createCollection(<name>, {capped: <boolean>, autoIndexId: <boolean>, size: <number>, max: <number>, storageEngine: <document>, validator: <document>, validationLevel: <string>, validationAction: <string>, indexOptionDefaults: <document>, viewOn: <string>, pipeline: <pipeline>, collation: <document>, writeConcern: <document>})



Parameters:

Optional parameters:

Examples:

In the following example, we are working with the “gfg” database in which we are creating a new capped collection of name “student” with maximum document capacity “4” using createCollection() method.

db.createCollection("student", {capped:true, size:10000, max:4})

Now we insert the documents in the student collection:

After insert documents, we will display all the documents present in the student collection using find() method.

Now, if we try to insert one more document, it will override the existing document. In the example, we are inserting a new document with the name Akash that overrides the existing document with the name Mihir:

After inserting one or more document:

How to check if the collection is capped or not?

We can check whether the collection is capped or not with the isCapped() method in MongoDB. This method returns true is the specified collections capped collection. Otherwise, return, false.

Syntax:

db.Collection_name.isCapped()

Example:

db.student.isCapped()

After convert student collection to a capped:

How to convert a collection to a capped?

If there is an existing collection that we want to change it to capped, we can do it using the convertToCapped command. It changes the current collection to capped collection:

Syntax:

    convertToCapped: <collection>,

    size: <capped size>,

    writeConcern: <document>,

    comment: <any>

}

Example:

db.runCommand({"convertToCapped":"student",size:5000})

Before convert to capped:

After convert to capped:

Advantages of Capped Collections

Disadvantages of Capped Collections

{ emptycapped: Collection_name }
Article Tags :