Capped Collections

Jun 1 • Posted 5 years ago

With MongoDB one may create collections of a predefined size, where old data automatically ages out on a least recently inserted basis.  This can be quite handy.  In the mongo JavaScript shell, it is as simple as this:

db.createCollection("mycoll", {capped: true, size:100000})

When capped, a MongoDB collection has a couple of interesting properties.  First, the data automatically ages out when the collection is full on a least recently inserted basis.

Second, for capped collections, MongoDB automatically keeps the objects in the collection in their insertion order.  This is great for logging-types of problems where order should be preserved.  To retrieve items in their insertion order:

db.mycoll.find().sort( {$natural:1} ); // oldest to newest
db.mycoll.find().sort( {$natural:-1} ); // newest to oldest

The implementation of the above two properties in the database is done at a low level and is very fast and efficient.  We could simulate this behavior by using a timestamp column and index, but with a significant speed penalty.

In fact, the capped collection performance is so good that MongoDB uses capped collections as the storage mechanism for its own replication logs. One can inspect these logs with standard MongoDB commands.  For example, if you have a master MongoDB database running, try this from the mongo shell:

use local
db.db.oplog.$main.find(); // show some replication log data
db.getReplicationInfo(); // distill from it some summary statistics
db.getReplicationInfo; // shows the js code for getReplicationinfo function

See the documentation for more information.

blog comments powered by Disqus
blog comments powered by Disqus