$dev4life = "Software Development Blog"

MongoDB - Capped Collections

- Create collection
- View collection stats
- Convert existing collection to capped

One of the best features of MongoDB is ability to create capped collections.  Capped collection is a fixed size collection that deletes the old data while the new one is added.  This process is done seamlessly in the background.  Anyone familiar with MySQL and large data sets (over few Gigs) will know the frustration.  Administrator could simply set a collection to be of certain size (in bytes) and the database will take care of everything else.  There are however couple of things to note.  As of right now, capped collections can not be sharded.  Also, MongoDB pre-allocated space for capped collection.

Example
// Create collection
db.createCollection("normal_collection");

// Convert to capped collection (50MB capped)
db.normal_collection.convertToCapped(50000000);

// Check if converted to capped
db.normal_collection.isCapped();

// Create a new capped collection
db.createCollection("capped_collection", { capped: true, size: 50000000 });

// Check collection stats (shows size, count, index information, etc)
db.normal_collection.stats();

mongodb,capped collection, convertToCapped, createCollection, database, isCapped, stats

Post a Comment