Flexibility, scalability, and easy usage have helped MongoDB, a document-oriented database rise to popularity. Written in C++, this open-source NoSQL database has a sharding and aggregation framework, coupled with fine-grained locking, document validations, and all solutions required to meet the complex requirements of modern applications. Capped collections in MongoDB allow a user to control collection size.   

What Is a Capped Collection in MongoDB? 

Fixed-size collections are called capped collections in MongoDB. While creating a collection, the user must specify the collection’s maximum size in bytes and the maximum number of documents that it would store. If more documents are added than the specified capacity, the existing ones are overwritten. 

Capped collection in MongoDB supports high-throughput operations used for insertion and retrieval of documents based on the order of insertion. Capped collection working is similar to circular buffers, i.e., there is a fixed space allocated for the capped collection. The oldest documents are overwritten to make space for the new documents in the collection once the fixed size gets exhausted.

Creating Capped Collection in MongoDB

Use the createCollection command along with the capped option to create a capped collection. Specify the collection’s maximum size in bytes as follows:

>db.createCollection("cappedLogCollection",{capped:true,size:10000})

To limit the number of documents that can be included in the capped collection, use the max parameter as shown below:

>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})

Querying Capped Collection in MongoDB

In a capped collection, the find query display results in insertion order by default. To retrieve the documents from a capped collection in the reverse order, use the sort command as follows: 

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

How to Check Whether a Collection is Capped or Not?

You can check whether a collection is capped using the isCapped() method. This method returns true as the output if the specified collection is a capped one. Otherwise, it returns false.

To check whether a given collection is capped or not, use the isCapped function as follows:

>db.cappedLogCollection.isCapped()

How to Convert a Collection to a Capped?

If there is a normal collection, you can change it to capped by using the convertToCapped command. To convert an existing collection to capped, use the following code:

>db.runCommand({"convertToCapped":"posts",size:10000})

Advantages of Capped Collections in MongoDB

  • It returns documents in insertion order without the need for an index and provides greater insertion throughput.
  • Capped collections enable changes that match the original document size, ensuring the position of the document does not change on the disk.
  • Holding log files.

Disadvantages of Capped Collections

  • It cannot be shared.
  • An update operation fails when the document exceeds the original size of the capped collection in MongoDB.
  • It is not possible to delete documents from a capped collection. You can delete all records using the following command: { emptycapped: Collection_name }

Conclusion

Ace MongoDB by advancing your skill set. Tread on the path to success by becoming a better data professional with Caltech Post Graduate Program in Data Science. Learn from IBM experts and Purdue faculty to lift your confidence and outshine in your upcoming interview. 

Our Big Data Courses Duration And Fees

Big Data Courses typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Post Graduate Program in Data Engineering

Cohort Starts: 5 Apr, 2024

8 Months$ 3,850

Learn from Industry Experts with free Masterclasses

  • Program Overview: The Reasons to Get Certified in Data Engineering in 2023

    Big Data

    Program Overview: The Reasons to Get Certified in Data Engineering in 2023

    19th Apr, Wednesday10:00 PM IST
  • Program Preview: A Live Look at the UCI Data Engineering Bootcamp

    Big Data

    Program Preview: A Live Look at the UCI Data Engineering Bootcamp

    4th Nov, Friday8:00 AM IST
  • 7 Mistakes, 7 Lessons: a Journey to Become a Data Leader

    Big Data

    7 Mistakes, 7 Lessons: a Journey to Become a Data Leader

    31st May, Tuesday9:00 PM IST
prevNext