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. Show
Get Certified With IBM and UCI DCE!UCI Data Engineering BootcampEnroll NowWhat 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 MongoDBUse 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 MongoDBIn 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 ScienceLearn the Fundamentals of Data ScienceEnroll NowHow 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
Disadvantages of Capped Collections
Related Topics
Simplilearn's Professional Certificate Program in Data Science in partnership with Purdue University and in collaboration with IBM, is ranked #1 Post Graduate in Data Science program by ET. If you wish to ace data science, this program is just the one for you! ConclusionAce 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 AuthorSimplilearnSimplilearn 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. |