Web Programming TutorialsLearn about Capped Collections in MongoDB

Learn about Capped Collections in MongoDB

Capped Collections

In this chapter, we are going to learn about the concept of Capped collections in MongoDB. When we insert documents in a MongoDB’s collection that does not have a fixed size allocated as disk memory, then over a period of time we may face performance issues for creation, reading, and deletion operations in the MongoDB due to space crunch. Therefore, the MongoDB is equipped with the capped collections which are the fixed-size circular collections. Such collections follow the insertion order that helps support high performance for the creation, reading, and deletion operations in the MongoDB. Being a circular and fixed sized collection, the MongoDB will start deleting the oldest document from the capped collection when the collection size is exhausted and such deletion does not require any of the MongoDB’s deletion commands to be executed explicitly.

Due to the circular and the fixed size nature of the capped collections, there are some restrictions to the update operation. If the updating of any document in the collection results in the increase of the document’s size, then Mongodb will not update this document in that collection because documents in the capped collections are stored in the order of the disk storage which ensures that the size of a single document does not exceed its allocated size on the disk. Therefore, the capped collections in MongoDB are suitable to store cache data, log information, or any other high volume data that need to be refreshed periodically.

How to create a Capped Collections in the MongoDB?
In MongoDB, we can create a capped collection by using the same syntax as we used to create a normal collection (i.e. createCollection command), but with a capped option as true and stipulating the maximum size of collection in terms of bytes as shown below.

>db.createCollection("cappedCollectionForCache",{capped:true,size:20000})

Here, the capped collection with the name as ‘cappedCollectionForCache’ has the disk size of 20,000 bytes. In addition to the above collection size, we could limit the number of documents to be present in the collection with the use of the max parameter as shown below.

>db.createCollection("cappedCollectionForCache",{capped:true,size:20000,max:3000})

As the max parameter has the value as 3000, it signifies that it can store maximum of 3000 documents only within the disk size of 20,000 bytes.
Example: –

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test

>db.createCollection("cappedCollectionForCache",{capped:true,size:20000,max:3000})
{ "ok" : 1 }
>

Check the Capped collection is capped or not?
The MongoDB provides the flexibility to check that a particular collection is capped or not after execution of the following isCapped command.

>db.cappedCollectionForCache.isCapped()

Example: –

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test

>db.createCollection("cappedCollectionForCache",{capped:true,size:20000,max:3000})
{ "ok" : 1 }
>
> db.cappedCollectionForCache.isCapped()
true
>

Converting an existing collection to a capped collection in MongoDB
MongoDB provides users with flexibility to convert an existing non capped collection into a capped collection after execution of the following command. Say we have an existing non capped collection with the name as “existingCollection” and we want to convert this collection into a capped collection with the name as “cappedCacheCollection”, then the following command will serve the purpose.

>db.runCommand({"cappedCacheCollection":" existingCollection",size:10000})

Querying a capped collection from MongoDB
In MongoDB, we can retrieve the documents present in the capped collection with the help of the find query as shown below. By default, it will always display the document results in the insertion order.

>db.cappedCollectionForCache.find()

Not only this, the MongoDB also provides the flexibility to retrieve the documents in the reverse order for which we have to use the sort command as shown below.

>db.cappedCollectionForCache.find().sort({$natural:-1})

Points to remember about Capped Collections
The following are the few points to remember about the use of the Capped Collections in the MongoDB.

  • If you are looking to delete a document from a capped collection, then you are looking in the wrong direction as we cannot delete documents from a capped collection. The old documents in a capped collection can only be deleted automatically upon insertion of the new documents when the allocated size to the capped collection has been exhausted.
  • It may sound strange, but it is true that there are no default indexes present in a capped collection. Not only this, but there is no index on the _id field as well.
    1. When you execute an insert operation on a capped collection, then MongoDB does not waste time to look for a place to lodge a new document on the disk. It may happen that MongoDB can blindly insert the new document at the tail of the capped collection. Since there is no waste of time in the disk space organization therefore, it makes the insert operations in capped collections execute very quickly.
  • Similarly, when you read the documents from a capped collection, then MongoDB returns the documents following the same order as present on the disk. Therefore, it makes the read operations on the capped collection to execute very fast.
  • Also, the update operation has a restriction. If the update of any document in the collection results in the increase of the document’s size, then Mongodb will not update that document in the collection as each document has allocated the fixed size during its first time insertion into the capped collection.

Application of Capped Collections

  • To store the cache data that need to be refreshed at a particular point of time.
  • To store the log information to control the verbosity of logs and prevent wastage of space on disk, which is occupied by the logs that may hamper the operational performance of the MongoDB due to potential disk space crunch.
  • To store any other high volume data that needs to be recycled periodically.

Conclusion: –
In this chapter, we discussed the concept of capped collections in the MongoDB along with the suitable examples to create, check capped or not, converting an existing collection to a capped collection and querying a capped collection.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -