[MongoDB]: Capped Collection

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

 

Capped collections have the following behaviors:

 

* Capped collections guarantee preservation of the insertion order. As a result, queries do not need an index to return documents in insertion order. Without this indexing overhead, they can support higher insertion throughput.

* Capped collections guarantee that insertion order is identical to the order on disk (natural order) and do so by prohibiting updates that increase document size. Capped collections only allow updates that fit the original document size, which ensures a document does not change its location on disk.

* Capped collections automatically remove the oldest documents in the collection without requiring scripts or explicit remove operations

 

Create a Capped Collection

 

You must create capped collections explicitly using the createCollection() method, which is a helper in the mongo shell for the create command.

When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection.

The size of the capped collection includes a small amount of space for internal overhead.

 

 

// let’s create a 1 MB Capped collections as below in a database say dbversity.

 

> db.createCollection(‘logs_collection’, {capped: true, size: 1048576})

 

2015-03-26T04:48:11.383-0400 [conn1] allocating new ns file /dbversity.ns, filling with zeroes…

2015-03-26T04:48:11.666-0400 [FileAllocator] allocating new datafile /dbversity.0, filling with zeroes…

2015-03-26T04:48:12.070-0400 [FileAllocator] done allocating datafile /dbversity.0, size: 64MB,  took 0.403 secs

2015-03-26T04:48:12.070-0400 [conn1] build index on: dbversity.logs_collection properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “dbversity.logs_collection” }

2015-03-26T04:48:12.071-0400 [conn1]     added index to empty collection

2015-03-26T04:48:12.071-0400 [conn1] command dbversity.$cmd command: create { create: “logs_collection”, capped: true, size: 1048576.0 } keyUpdates:0 numYields:0 locks(micros) w:687636 reslen:37 687ms

{ “ok” : 1 }

>

 

// If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.

 

 

> show dbs

admin      (empty)

dbversity  0.078GB

local      0.078GB

>

> show collections

logs_collection

system.indexes

>

 

// Capped collections created after 2.2 have an _id field and an index on the _id field by default, see below.

// Capped collections created before 2.2 do not have an index on the _id field by default.

// If you are using capped collections with replication prior to 2.2, you should explicitly create an index on the _id field.

 

 

> db.system.indexes.find()

{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “dbversity.logs_collection” }

>

> db.logs_collection.find()

>

> for(var i=1; i <= 1000000 ; i++){db.logs_collection.insert({ _id : i, log_content : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “,  iteration : i, occurance : i })}

>

 

 

// At another console while the for loop in progress

 

> db.logs_collection.find()

{ “_id” : 202641, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202641, “occurance” : 202641 }

{ “_id” : 202642, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202642, “occurance” : 202642 }

{ “_id” : 202643, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202643, “occurance” : 202643 }

{ “_id” : 202644, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202644, “occurance” : 202644 }

{ “_id” : 202645, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202645, “occurance” : 202645 }

{ “_id” : 202646, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202646, “occurance” : 202646 }

{ “_id” : 202647, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202647, “occurance” : 202647 }

{ “_id” : 202648, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202648, “occurance” : 202648 }

{ “_id” : 202649, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202649, “occurance” : 202649 }

{ “_id” : 202650, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202650, “occurance” : 202650 }

{ “_id” : 202651, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202651, “occurance” : 202651 }

{ “_id” : 202652, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202652, “occurance” : 202652 }

{ “_id” : 202653, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202653, “occurance” : 202653 }

{ “_id” : 202654, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202654, “occurance” : 202654 }

{ “_id” : 202655, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202655, “occurance” : 202655 }

{ “_id” : 202656, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202656, “occurance” : 202656 }

{ “_id” : 202657, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202657, “occurance” : 202657 }

{ “_id” : 202658, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202658, “occurance” : 202658 }

{ “_id” : 202659, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202659, “occurance” : 202659 }

{ “_id” : 202660, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 202660, “occurance” : 202660 }

Type “it” for more

>

> db.logs_collection.find()

{ “_id” : 208746, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208746, “occurance” : 208746 }

{ “_id” : 208747, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208747, “occurance” : 208747 }

{ “_id” : 208748, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208748, “occurance” : 208748 }

{ “_id” : 208749, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208749, “occurance” : 208749 }

{ “_id” : 208750, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208750, “occurance” : 208750 }

{ “_id” : 208751, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208751, “occurance” : 208751 }

{ “_id” : 208752, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208752, “occurance” : 208752 }

{ “_id” : 208753, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208753, “occurance” : 208753 }

{ “_id” : 208754, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208754, “occurance” : 208754 }

{ “_id” : 208755, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208755, “occurance” : 208755 }

{ “_id” : 208756, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208756, “occurance” : 208756 }

{ “_id” : 208757, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208757, “occurance” : 208757 }

{ “_id” : 208758, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208758, “occurance” : 208758 }

{ “_id” : 208759, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208759, “occurance” : 208759 }

{ “_id” : 208760, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208760, “occurance” : 208760 }

{ “_id” : 208761, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208761, “occurance” : 208761 }

{ “_id” : 208762, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208762, “occurance” : 208762 }

{ “_id” : 208763, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208763, “occurance” : 208763 }

{ “_id” : 208764, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208764, “occurance” : 208764 }

{ “_id” : 208765, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 208765, “occurance” : 208765 }

Type “it” for more

>

> db.logs_collection.find()

{ “_id” : 217007, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217007, “occurance” : 217007 }

{ “_id” : 217008, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217008, “occurance” : 217008 }

{ “_id” : 217009, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217009, “occurance” : 217009 }

{ “_id” : 217010, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217010, “occurance” : 217010 }

{ “_id” : 217011, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217011, “occurance” : 217011 }

{ “_id” : 217012, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217012, “occurance” : 217012 }

{ “_id” : 217013, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217013, “occurance” : 217013 }

{ “_id” : 217014, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217014, “occurance” : 217014 }

{ “_id” : 217015, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217015, “occurance” : 217015 }

{ “_id” : 217016, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217016, “occurance” : 217016 }

{ “_id” : 217017, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217017, “occurance” : 217017 }

{ “_id” : 217018, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217018, “occurance” : 217018 }

{ “_id” : 217019, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217019, “occurance” : 217019 }

{ “_id” : 217020, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217020, “occurance” : 217020 }

{ “_id” : 217021, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217021, “occurance” : 217021 }

{ “_id” : 217022, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217022, “occurance” : 217022 }

{ “_id” : 217023, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217023, “occurance” : 217023 }

{ “_id” : 217024, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217024, “occurance” : 217024 }

{ “_id” : 217025, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217025, “occurance” : 217025 }

{ “_id” : 217026, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 217026, “occurance” : 217026 }

Type “it” for more

>

>

> db.logs_collection.find()

{ “_id” : 345514, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345514, “occurance” : 345514 }

{ “_id” : 345515, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345515, “occurance” : 345515 }

{ “_id” : 345516, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345516, “occurance” : 345516 }

{ “_id” : 345517, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345517, “occurance” : 345517 }

{ “_id” : 345518, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345518, “occurance” : 345518 }

{ “_id” : 345519, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345519, “occurance” : 345519 }

{ “_id” : 345520, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345520, “occurance” : 345520 }

{ “_id” : 345521, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345521, “occurance” : 345521 }

{ “_id” : 345522, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345522, “occurance” : 345522 }

{ “_id” : 345523, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345523, “occurance” : 345523 }

{ “_id” : 345524, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345524, “occurance” : 345524 }

{ “_id” : 345525, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345525, “occurance” : 345525 }

{ “_id” : 345526, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345526, “occurance” : 345526 }

{ “_id” : 345527, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345527, “occurance” : 345527 }

{ “_id” : 345528, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345528, “occurance” : 345528 }

{ “_id” : 345529, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345529, “occurance” : 345529 }

{ “_id” : 345530, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345530, “occurance” : 345530 }

{ “_id” : 345531, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345531, “occurance” : 345531 }

{ “_id” : 345532, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345532, “occurance” : 345532 }

{ “_id” : 345533, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 345533, “occurance” : 345533 }

Type “it” for more

 

> db.logs_collection.find().count()

4944

> db.logs_collection.find().count()

4944

> db.logs_collection.find().count()

4944

>

 

#

[Dev root @ myhostname /opt/mongodb/bin]# ll -lhtr /dbversity.*

-rw——- 1 root root 16M Mar 26 04:59 /dbversity.ns

-rw——- 1 root root 64M Mar 26 04:59 /dbversity.0

[Dev root @ myhostname /opt/mongodb/bin]#

[Dev root @ myhostname /opt/mongodb/bin]# ll -lhtr /dbversity.*

-rw——- 1 root root 16M Mar 26 04:59 /dbversity.ns

-rw——- 1 root root 64M Mar 26 04:59 /dbversity.0

[Dev root @ myhostname /opt/mongodb/bin]#

[Dev root @ myhostname /opt/mongodb/bin]#

[Dev root @ myhostname /opt/mongodb/bin]# ll -lhtr /dbversity.*

-rw——- 1 root root 16M Mar 26 04:59 /dbversity.ns

-rw——- 1 root root 64M Mar 26 04:59 /dbversity.0

[Dev root @ myhostname /opt/mongodb/bin]

 

 

 

 

 

// Post completion of for loop

 

> db.logs_collection.find().limit(2)

{ “_id” : 995057, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 995057, “occurance” : 995057 }

{ “_id” : 995058, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 995058, “occurance” : 995058 }

>

 

 

> db.logs_collection.find( { _id : { $gte : 999999 } } )

{ “_id” : 999999, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 999999, “occurance” : 999999 }

{ “_id” : 1000000, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 1000000, “occurance” : 1000000 }

>

 

 

Recommendations and Restrictions

 

You can only make in-place updates of documents. If the update operation causes the document to grow beyond their original size, the update operation will fail.

If you plan to update documents in a capped collection, create an index so that these update operations do not require a table scan.

 

 

>  db.logs_collection.update({},{ $set: {log_content : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection ” }}, {multi : true})

WriteResult({

“nMatched” : 0,

“nUpserted” : 0,

“nModified” : 0,

“writeError” : {

“code” : 10003,

“errmsg” : “failing update: objects in a capped ns cannot grow”

}

})

>

>

 

If you update a document in a capped collection to a size smaller than its original size, and then a secondary resyncs from the primary, the secondary will replicate and allocate space based on the current smaller document size.

If the primary then receives an update which increases the document back to its original size, the primary will accept the update but the secondary will fail with a failing update: objects in a capped ns cannot grow error message.

 

To prevent this error, create your secondary from a snapshot of one of the other up-to-date members of the replica set. Follow  tutorial on filesystem snapshots at http://docs.mongodb.org/manual/tutorial/backup-with-filesystem-snapshots/ to seed your new secondary.

Seeding the secondary with a filesystem snapshot is the only way to guarantee the primary and secondary binary files are compatible.

MMS Backup snapshots are insufficient in this situation since you need more than the content of the secondary to match the primary.

 

 

You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection.

 

> db.logs_collection.remove()

2015-03-26T05:29:33.544-0400 remove needs a query at src/mongo/shell/collection.js:299

>

> db.logs_collection.drop()

2015-03-26T05:35:31.084-0400 [conn1] CMD: drop dbversity.logs_collection

true

>

> db.logs_collection.find()

>

>

 

 

We’ll create a new capped collection with maximum number of documents for the collection using the max field as in the following document:

 

 

>  db.createCollection(‘logs_collection’, {capped: true, size: 1048576, max : 10 })

2015-03-26T05:37:26.382-0400 [conn1] build index on: dbversity.logs_collection properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “dbversity.logs_collection” }

2015-03-26T05:37:26.383-0400 [conn1]     added index to empty collection

{ “ok” : 1 }

>

 

 

> for(var i=1; i <= 20 ; i++){db.logs_collection.insert({ _id : i, log_content : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “,  iteration : i, occurance : i })}

WriteResult({ “nInserted” : 1 })

>

>

> db.logs_collection.find()

{ “_id” : 11, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 11, “occurance” : 11 }

{ “_id” : 12, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 12, “occurance” : 12 }

{ “_id” : 13, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 13, “occurance” : 13 }

{ “_id” : 14, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 14, “occurance” : 14 }

{ “_id” : 15, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 15, “occurance” : 15 }

{ “_id” : 16, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 16, “occurance” : 16 }

{ “_id” : 17, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 17, “occurance” : 17 }

{ “_id” : 18, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 18, “occurance” : 18 }

{ “_id” : 19, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 19, “occurance” : 19 }

{ “_id” : 20, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 20, “occurance” : 20 }

>

 

 

 

IMPORTANT : The size argument is always required, even when you specify max number of documents. MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

 

Query a Capped Collection

If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.

 

To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1, as shown in the following example:

 

> db.logs_collection.find().sort( { $natural: -1 } )

{ “_id” : 20, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 20, “occurance” : 20 }

{ “_id” : 19, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 19, “occurance” : 19 }

{ “_id” : 18, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 18, “occurance” : 18 }

{ “_id” : 17, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 17, “occurance” : 17 }

{ “_id” : 16, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 16, “occurance” : 16 }

{ “_id” : 15, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 15, “occurance” : 15 }

{ “_id” : 14, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 14, “occurance” : 14 }

{ “_id” : 13, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 13, “occurance” : 13 }

{ “_id” : 12, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 12, “occurance” : 12 }

{ “_id” : 11, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : logs_collection “, “iteration” : 11, “occurance” : 11 }

>

>

 

 

Check if a Collection is Capped

Use the isCapped() method to determine if a collection is capped, as follows:

 

 

> db.logs_collection.isCapped()

true

>

 

 

Convert a Collection to Capped

You can convert a non-capped collection to a capped collection with the convertToCapped command:

 

 

 

>  for(var i=1; i <= 20 ; i++){db.nonCapped_col.insert({ _id : i, log_content : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “,  iteration : i, occurance : i })}

 

2015-03-26T05:42:48.748-0400 [conn1] build index on: dbversity.nonCapped_col properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “dbversity.nonCapped_col” }

2015-03-26T05:42:48.748-0400 [conn1]     added index to empty collection

WriteResult({ “nInserted” : 1 })

>

> db.nonCapped_col.isCapped()

false

>

 

> db.runCommand({“convertToCapped”: “nonCapped_col”, size: 1024});

2015-03-26T05:44:23.668-0400 [conn1] build index on: dbversity.tmp.convertToCapped.nonCapped_col properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “dbversity.tmp.convertToCapped.nonCapped_col” }

2015-03-26T05:44:23.668-0400 [conn1]     added index to empty collection

{ “ok” : 1 }

>

>

> db.nonCapped_col.isCapped()

true

>

> db.nonCapped_col.find()

{ “_id” : 4, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 4, “occurance” : 4 }

{ “_id” : 5, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 5, “occurance” : 5 }

{ “_id” : 6, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 6, “occurance” : 6 }

{ “_id” : 7, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 7, “occurance” : 7 }

{ “_id” : 8, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 8, “occurance” : 8 }

{ “_id” : 9, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 9, “occurance” : 9 }

{ “_id” : 10, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 10, “occurance” : 10 }

{ “_id” : 11, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 11, “occurance” : 11 }

{ “_id” : 12, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 12, “occurance” : 12 }

{ “_id” : 13, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 13, “occurance” : 13 }

{ “_id” : 14, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 14, “occurance” : 14 }

{ “_id” : 15, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 15, “occurance” : 15 }

{ “_id” : 16, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 16, “occurance” : 16 }

{ “_id” : 17, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 17, “occurance” : 17 }

{ “_id” : 18, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 18, “occurance” : 18 }

{ “_id” : 19, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 19, “occurance” : 19 }

{ “_id” : 20, “log_content” : “To insert the Dummy data for the Capped collection testing; Database Name : dbversity, Collection Name : nonCapped_col “, “iteration” : 20, “occurance” : 20 }

>

>

> db.nonCapped_col.dataSize()

3332

>

>

 

// Rememner : If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.

 

> db.nonCapped_col.storageSize()

4096

>

>

> db.nonCapped_col.stats()

{

“ns” : “dbversity.nonCapped_col”,

“count” : 17,

“size” : 3332,

“avgObjSize” : 196,

        “storageSize” : 4096,

“numExtents” : 1,

“nindexes” : 1,

“lastExtentSize” : 4096,

“paddingFactor” : 1,

“systemFlags” : 1,

“userFlags” : 0,

“totalIndexSize” : 8176,

“indexSizes” : {

“_id_” : 8176

},

“capped” : true,

“max” : NumberLong(“9223372036854775807”),

“ok” : 1

}

>

>

> db.logs_collection.stats()

{

“ns” : “dbversity.logs_collection”,

“count” : 10,

“size” : 1960,

“avgObjSize” : 196,

        “storageSize” : 1048576,

“numExtents” : 1,

“nindexes” : 1,

“lastExtentSize” : 1048576,

“paddingFactor” : 1,

“systemFlags” : 1,

“userFlags” : 0,

“totalIndexSize” : 8176,

“indexSizes” : {

“_id_” : 8176

},

“capped” : true,

“max” : 10,

“ok” : 1

}

>

  • Ask Question