MongoDB Capped Collections

MongoDB Capped Collections is a collection with excellent performance and fixed size. For a fixed size, we can imagine it as a circular queue. When the collection space is used up, the inserted elements will overwrite the initial Elements of the head!

Creating a fixed collection

We create a fixed collection through createCollection and set the capped option to true:


You can also specify the number of documents, plus the max: 1000 attribute:


Determine if the collection is a fixed collection:


If you need to convert an existing collection to a fixed collection, you can use the following command´╝Ü


The above code converts our existing posts collection into a fixed collection.

Fixed set query

Fixed collection documents are stored in insertion order. By default, queries are returned in insertion order. You can also use $ natural to adjust the return order.


Functional characteristics of fixed collections

You can insert and update, but the update cannot exceed the size of the collection, otherwise the update fails and delete is not allowed, but you can call drop () to delete all rows in the collection, but you need to explicitly rebuild the collection after the drop.

The maximum value of a cappped collection on a 32-bit machine is about 482.5M. On a 64-bit machine, it is limited only by the size of the system file.

Fixed collection attributes and usage


  • Attribute 1: extremely fast insertion of fixed collections
  • Attribute 2: query output in insert order is extremely fast
  • Attribute 3: the ability to eliminate the oldest data when inserting the latest data


  • Usage 1: store log information
  • Usage 2: cache some small documents

Welookups is optimized for learning. © 2018 -

All Right Reserved and you agree to have read and accepted our term and condition.