EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Capped Collections
 

MongoDB Capped Collections

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

MongoDB Capped Collections

 

 

Introduction to MongoDB Capped Collections

MongoDB Capped Collections is nothing but the circular collection’s fixed size, which followed the insertion to support high performance for creating, delete and read operations. Circular capped collection states that when we allocate the fixed size to the collection, it was exhausted. Capped Collection will delete the oldest document from the collection, to delete this document we have not to provide any explicit command for the same. The capped collection is essential and useful to provide a fixed size for collection, which followed the insertion order.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

db.createCollection (Collection_name, {capped: Boolean, autoIndexId: Boolean, size: number, max:number})

Parameters:

Below is the parameter description syntax of capped collections in MongoDB.

1. Create collection: Create collection is a method used to create a new capped collection.

2. Collection name: It states the capped collection name. We have used any name for the capped collection.

3. Capped: We have set capped option either true or false if we have set the capped option is true then our collection will be created as a capped collection. If we have specified the capped collection option as false, then the collection is created without capped collection.

4. Size: Size option will specify the limit of size for the capped collection. We can specify the size of the capped collection in bytes. This parameter is mandatory when we have defined our collection as a capped collection in MongoDB.

5. Max: Max option will specify the limit of the maximum number of documents allowed in the capped collection. Size option will give preference over the max option in MongoDB capped collection.

6. Auto index ID: We have set auto index id either true or false. If we have set the auto index ID parameter is false at that time capped collection will disable the automatic index creation. If we set it to the truth, then it will create an automatic index on capped collections.

How Capped Collections Work in MongoDB?

Below is the working of the capped collection in MongoDB.

  • It is nothing but the circular collection’s fixed size, which followed the insertion to support high performance for creating, delete, and read operations.
  • The capped collection will restrict the update when the update result reaches the document size.
  • The capped collection will store the documents in the order of disk storage. When the max limit is exceeded, then insertion and updation are not allowed on capped collection.
  • The caped collection will ensure that the collection’s size will not increase the allocated size on the disk.
  • Capped collection in MongoDB is basically used to store log information, the high volume of data, and cache information.
  • Circular capped collection states that when we allocate the fixed size to the collection, it was exhausted. At that time capped collection in MongoDB will delete the oldest document from the collection.
  • To delete the document, we do not need to provide any explicit command for the same.
  • We have used normal capped collection command for creating a capped collection, but we need to specify the capped option is true and need to specify the maximum size of the collection in bytes.
  • We have used Capped command to check our collection is capped or not. It will display the flag as true or false. If our collection is capped, it will display true, if our collection is not capped, it will display the false. Below is the example:

Code:

db.books.isCapped()
db.capped_test.isCapped()

Output:

MongoDB Capped Collections Example 1

Explanation: In the above example books collection is not capped collection so it will display the flag as false, capped_test is capped collection so it will display the flag as true. The capped collection will make room for new documents while reaching the limit of the capped collection. The capped collection will preserve the insertion order, without indexing overhead capped collection will support higher insertion throughout in MongoDB.

Examples of MongoDB Capped Collections

Below is the example of the capped collection in MongoDB.

Example #1 – Create Capped Collection

We have created a capped collection name as capped_test_mongodb.

Code:

db.createCollection("capped_test_mongodb",{capped:true,size:1})
db.capped_test_mongodb.isCapped()

Output:

MongoDB Capped Collections Example 2

Explanation: In the above example, we have used the size parameter as 1, so the size of the capped collection is 1 byte. After creating capped collection we have checked the created collection is capped or not.

Example #2 – Using Max Parameter

We have created a capped collection name as capped_max_mongodb. We have added the size and max parameter while creating a collection in MongoDB. The below example shows that create a collection using max and size parameters.

Code:

db.createCollection ("capped_max_mongodb", {capped: true, size: 10, max: 10})
db.capped_max_mongodb.isCapped ()

Output:

Max Parameter Example 3

Explanation: In the above example, we have used size parameter as 10 and max parameter as 10, so the size of the capped collection is 10 bytes and max documents are 10.

Example #3 – Convert collection into the capped collection

We have converted books collection into the capped collection. We have added the size and max parameter while converting collection into the capped collection in MongoDB.

Code:

db.books.isCapped ()
db.runCommand({"convertToCapped":"books", size:10, max:10})

Output:

Max Parameter Example 4

Explanation: In the above example, we have used size parameter as 10 and max parameter as 10, so the size of the capped collection is 10 bytes and max documents are 10.

Recommended Article

This is a guide to MongoDB Capped Collections. Here we discuss the Introduction to MongoDB Capped Collections and its different parameters along with its examples. You can also go through our other suggested articles to learn more –

  1. How Does Collection Method Works in MongoDB?
  2. MongoDB Alternatives | Top 8
  3. Lookup in MongoDB With Characteristics
  4. MongoDB Limit() | How to Works?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW