{"id":11408,"date":"2014-01-26T19:00:36","date_gmt":"2014-01-26T13:30:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11408"},"modified":"2014-01-26T19:00:36","modified_gmt":"2014-01-26T13:30:36","slug":"capped-collections-in-mongodb","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/capped-collections-in-mongodb\/","title":{"rendered":"Capped Collections In Mongodb"},"content":{"rendered":"<p>We can create collections in mongoDb on which we can apply size limit. These special type of collections are called <strong>Capped Collections<\/strong>. These are a kind of circular queues, in which if allocated size limit is reached, it makes space for new documents by overwriting the oldest documents in the collection. <\/p>\n<p><strong>How to create Capped Collections:<\/strong><br \/>\n<code>db.createCollection(nameOfCollection, {capped: Boolean, autoIndexId: Boolean, size: Number, max : Number})<\/code><br \/>\nHere the parameters specify:<\/p>\n<ul>\n<li>nameOfCollection: the capped collection name<\/li>\n<li>&#8220;capped&#8221; option : set to true in case to create a capped collection<\/li>\n<li>&#8220;size&#8221; option: specify the size limit for the collection in bytes(mandatory in case of capped collection)\n<li>&#8220;max&#8221; option: specify the limit on maximum number of documents allowed in the collection(size option is given preference over max option)<\/li>\n<li>&#8220;autoIndexId&#8221; option: set to false in case of capped collections to disable the automatic index creation<\/li>\n<\/ul>\n<p> For example lets create a capped collection named &#8220;demo&#8221;.<br \/>\n<img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/01\/Screenshot-from-2014-01-26-173904.png\" alt=\"create capped collection\" \/><\/p>\n<p>Here we have put size limit to 500 bytes and maximum number of documents to 3. Now lets insert some documents in this collection.<br \/>\n<img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/01\/Screenshot-from-2014-01-26-174108.png\" alt=\"\" \/><\/p>\n<p>Now, the maximum size limit has reached and if we try to add more documents, the old documents get over ridden by new one.<br \/>\n<img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/01\/Screenshot-from-2014-01-26-174202.png\" alt=\"\" \/><\/p>\n<p><strong>Note:<\/strong> <code>db.collection.isCapped()<\/code> command is used to check if a collection is a Capped collection or not.<\/p>\n<p><strong>Advantages of Capped Collections:<\/strong><br \/>\n1. Queries do not need an index to return documents in insertion order due to which it provide higher insertion throughput.<\/p>\n<p>2. Capped collections only allow updates that fit the original document size, which ensures a document does not change its location on disk.<\/p>\n<p>3. Useful for keeping log files.<\/p>\n<p><strong>Disadvantages of Capped Collections:<\/strong><br \/>\n1. If the update operation increases the original size of the capped collection, the update operation fails.<\/p>\n<p>2. We cannot delete documents from a capped collection. To remove all records from a capped collection, use the <code>{ emptycapped: nameOfCollection }<\/code> command.<\/p>\n<p>3. We cannot shard a capped collection.<\/p>\n<p>So we can make a choice whether to use capped collection according to our use case.<\/p>\n<p>Hope this will help \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can create collections in mongoDb on which we can apply size limit. These special type of collections are called Capped Collections. These are a kind of circular queues, in which if allocated size limit is reached, it makes space for new documents by overwriting the oldest documents in the collection. How to create Capped [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1270,4846],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11408"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11408"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11408\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}