WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

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:

>db.createCollection("cappedLogCollection",{capped:true,size:10000})

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

>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})

Determine if the collection is a fixed collection:

>db.cappedLogCollection.isCapped()

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

>db.runCommand({"convertToCapped":"posts",size:10000})

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.

>db.cappedLogCollection.find().sort({$natural:-1})

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

Attributes

  • 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

  • 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.