Contents of page >
1) What are capped collection in MongoDB?
Capped collections are of fixed size.
Features of capped collection >
- Capped collections maintains insertion order, so they provide very high performance for CRUD operations.
- Performance improvement during CRUD >
- Insert - Document is inserted directly at last so insertion is quick.
- Delete - Doesn’t support delete operation. For deletion you can only drop collection (which will delete all documents and collection also)
- Read/Query - As they maintain insertion order, they don’t need an index to return documents in insertion order.
- Update - For updation - It’s recommended to create an index so that all documents are not scanned for update.
- Default index on _id field.
- Capped collections are also called circular collections. Because once collection size is exhausted it starts deleting the oldest documents automatically.
2) Create capped collection in MongoDB >
db.createCollection("studentCappedCollection",{capped:true,size:9999})
|
capped:true means collection will be capped.
size:9999 is the size of collection in bytes.
3) Create capped collection with limit of documents in MongoDB>
db.createCollection("studentCappedCollection",{capped:true,size:9999, max : 10})
|
capped:true means collection will be capped.
size:9999 is the size of collection in bytes.
max:10 is the number of documents that collection can have.
4) Find out whether collection is capped or not in MongoDB ?
db.studentCappedCollection.isCapped()
|
Output>
true
|
5) How to convert non-capped collection to capped collection in MongoDB?
We will convert existing student collection into capped collection.
db.runCommand({"convertToCapped":"student",size:10000,max:99})
|
Now, student collection is a capped collection with
- Its maximum size can be 9999 bytes.
- Maximum number of documents it can have is 10.
6) query/find/Retrieve documents from capped collection in MongoDB>
First let’s insert in studentCappedCollection>
db.studentCappedCollection.insert({ _id: 1, X: "ankit"})
db.studentCappedCollection.insert({ _id: 2, X: "neh"})
|
Now, let's query documents >
db.studentCappedCollection.find()
|
Output>
{ "_id" : 1, "X" : "ankit" }
{ "_id" : 2, "X" : "neh" }
|
So, we see that capped collections maintains insertion order by default.
7) query/find/Retrieve documents in REVERSE order of insertion from capped collection in MongoDB>
Now, let's query documents >
db.studentCappedCollection.find().sort( { $natural: -1 } )
|
Output>
{ "_id" : 2, "X" : "neh" }
{ "_id" : 1, "X" : "ankit" }
|
So, we retrieved documents from capped collections in reverse order of insertion.
8) Summary -
So in this MongoDB tutorial we learned Capped collections are of fixed size. Capped collections maintains insertion order, so they provide very high performance for CRUD operations.
Create capped collection in MongoDB >
db.createCollection("studentCappedCollection",{capped:true,size:9999})
|
size:9999 is the size of collection in bytes.
Create capped collection with limit of documents in MongoDB>
db.createCollection("studentCappedCollection",{capped:true,size:9999, max : 10})
|
max:10 is the number of documents that collection can have.
Find out whether collection is capped or not in MongoDB ?
db.studentCappedCollection.isCapped()
|
convert non-capped collection to capped collection in MongoDB?
db.runCommand({"convertToCapped":"student",size:10000,max:99})
|
query capped collection in MongoDB>
db.studentCappedCollection.find()
|
query documents in REVERSE order of insertion from capped collection>
db.studentCappedCollection.find().sort( { $natural: -1 } )
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.
RELATED LINKS>
What is MongoDB - A quick introduction to database
Labels:
MongoDB