Capped Collections in MongoDB

By | May 17, 2018

What is Capped Collection?

As the name suggests, a collection created with a Cap [ Limit on Size and No. Of documents] are said to be Capped Collection.

Use Case:

Application Logging, History, Audit Info are the classic examples where Capped Collection will be of immense use.

How to create a Capped Collection ?

> db.createCollection("AuditTrail", {capped : true, size : 1000, max : 10 })

Size determines the document size

max determines maximum documents allowed.

Check if the collection is capped or not?

> db.AuditTrail.isCapped()

View documents from a Capped Collection

> db.AuditTrail.find().sort( {$natural : -1})
{ "_id" : ObjectId("5afd1d8f4fba74748720cbb2"), "Desc" : "User A LoggedIn", "Type" : "Info" }
{ "_id" : ObjectId("5afd1d2a4fba74748720cbb1"), "Desc" : "User Z LoggedIn", "Type" : "Info" }
{ "_id" : ObjectId("5afd19714fba74748720cbb0"), "Desc" : "User Y LoggedOut", "Type" : "Info" }
{ "_id" : ObjectId("5afd196c4fba74748720cbaf"), "Desc" : "User Y Completed Payment", "Type" : "Info" }
{ "_id" : ObjectId("5afd19604fba74748720cbae"), "Desc" : "User Y Checked Out his Cart", "Type" : "Info" }
{ "_id" : ObjectId("5afd194e4fba74748720cbad"), "Desc" : "User Y added Product A to Cart", "Type" : "Info" }
{ "_id" : ObjectId("5afd18f34fba74748720cbac"), "Desc" : "User Y Logged in Successfully", "Type" : "Info" }
{ "_id" : ObjectId("5afd18ec4fba74748720cbab"), "Desc" : "User Y LoggedOut", "Type" : "Info" }
{ "_id" : ObjectId("5afd18db4fba74748720cbaa"), "Desc" : "User Y browsed product catalog's", "Type" : "Info" }
{ "_id" : ObjectId("5afd18c94fba74748720cba9"), "Desc" : "User X LoggedOut", "Type" : "Info" }

Let’s add one more entry and see the results :

> db.AuditTrail.insert( {"Desc": "User P LoggedIn", "Type": "Info"} )
WriteResult({ "nInserted" : 1 })
>

Now’s the results :

> db.AuditTrail.find().sort( {$natural : -1})
{ "_id" : ObjectId("5afd31e24fba74748720cbb3"), "Desc" : "User P LoggedIn", "Type" : "Info" }
{ "_id" : ObjectId("5afd1d8f4fba74748720cbb2"), "Desc" : "User A LoggedIn", "Type" : "Info" }
{ "_id" : ObjectId("5afd1d2a4fba74748720cbb1"), "Desc" : "User Z LoggedIn", "Type" : "Info" }
{ "_id" : ObjectId("5afd19714fba74748720cbb0"), "Desc" : "User Y LoggedOut", "Type" : "Info" }
{ "_id" : ObjectId("5afd196c4fba74748720cbaf"), "Desc" : "User Y Completed Payment", "Type" : "Info" }
{ "_id" : ObjectId("5afd19604fba74748720cbae"), "Desc" : "User Y Checked Out his Cart", "Type" : "Info" }
{ "_id" : ObjectId("5afd194e4fba74748720cbad"), "Desc" : "User Y added Product A to Cart", "Type" : "Info" }
{ "_id" : ObjectId("5afd18f34fba74748720cbac"), "Desc" : "User Y Logged in Successfully", "Type" : "Info" }
{ "_id" : ObjectId("5afd18ec4fba74748720cbab"), "Desc" : "User Y LoggedOut", "Type" : "Info" }
{ "_id" : ObjectId("5afd18db4fba74748720cbaa"), "Desc" : "User Y browsed product catalog's", "Type" : "Info" }
>

Since We have a limit of 10 documents set while creating this capped collection, any further inserts removes the oldest entry automatically.

$natural helps to display the order in which documents are inserted in the cappedCollection.

Good to Know:

We can’t Shard Capped Collection

Indexing will help efficient update operations

No Delete option available on Capped Collection. Instead you can Drop() Capped Collection and Recreate

Hope this was helpful

Leave a Reply

Your email address will not be published. Required fields are marked *