Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Capped Collection in MongoDB

DZone's Guide to

Capped Collection in MongoDB

Read this article in order to discover what a capped collection is in MongoDB. It includes use cases and how to create a capped collection.

Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

What is Capped Collection?

As the name suggests, a collection created with a cap (limit on size and number of documents) is said to be capped collections.

Use Case:

Application logging, history, and audit info are the classic examples where capped collections will be of immense use.

Creating a Capped Collection:

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

Size determines the document size.

Max determines maximum documents allowed.

Let's find out 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 remove the oldest entry automatically.

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

Good to Know:

  1. We can’t shard capped collections
  2. Indexing will help efficient update operations
  3. There is no delete option available on capped collections. Instead, you can drop() capped collection and recreate

Hope that was useful.

Previous Articles:

https://dzone.com/articles/indexing-in-mongodb

https://dzone.com/articles/covered-query-mongodb

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
mongodb ,logging ,database ,capped collection

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}