Site icon Tutorial

Capped Collections

Capped Collections

Capped collections are fixed-size collections that support high-throughput operations that insert, retrieve, and delete 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:

For example, the oplog.rs collection that stores a log of the operations in a replica set uses a capped collection. Consider the following potential use cases for capped collections:

Recommendations and Restrictions

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.

db.createCollection( “log”, { capped: true, size: 100000 } )

Additionally, you may also specify a maximum number of documents for the collection using the max field as in the following document:

db.createCollection(“log”, { capped : true, size : 5242880, max : 5000 } )

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.cappedCollection.find().sort( { $natural: -1 } )

Check if a Collection is Capped. Use the isCapped() method to determine if a collection is capped, as follows: db.collection.isCapped()

Convert a Collection to Capped – You can convert a non-capped collection to a capped collection with the convertToCapped command:

db.runCommand({“convertToCapped”: “mycoll”, size: 100000});

The size parameter specifies the size of the capped collection in bytes.

Automatically Remove Data After a Specified Period of Time – For additional flexibility when expiring data, consider MongoDB’s TTL indexes, as described in Expire Data from Collections by Setting TTL. These indexes allow you to expire and remove data from normal collections using a special type, based on the value of a date-typed field and a TTL value for the index. TTL Collections are not compatible with capped collections.

Tailable Cursor – You can use a tailable cursor with capped collections. Similar to the Unix tail -f command, the tailable cursor “tails” the end of a capped collection. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.

Apply for MongoDB Certification Now!!

https://www.vskills.in/certification/databases/mongodb-server-administrator

Back to Tutorial

Exit mobile version