EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Capped Collections
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB Capped Collections

By Priya PedamkarPriya Pedamkar

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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?
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more