Cara menggunakan mongodb collection size limit

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

Cara menggunakan mongodb collection size limit

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

Cara menggunakan mongodb collection size limit

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 }
  1. Drop Collection in MongoDB
  2. Create Collection in MongoDB
  3. MongoDB Update Document
  4. Data Types In MongoDB 
  5. MongoDB Cheatsheet 
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!

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

Cara menggunakan mongodb collection size limit
Simplilearn

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.