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.   

Get Certified With IBM and UCI DCE!

UCI Data Engineering BootcampEnroll Now
Get Certified With IBM and UCI DCE!

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()

Free Course: Introduction to Data Science

Learn the Fundamentals of Data ScienceEnroll Now
Free Course: Introduction to Data Science

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 Professional Certificate Program in Data Science. Learn from IBM experts and Purdue faculty to lift your confidence and outshine in your upcoming interview. 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors