Latest web development tutorials

MongoDB fixed set (Capped Collections)

MongoDB fixed set (Capped Collections) performance is excellent and has a fixed size collection for a fixed size, we can imagine it as a circular queue, when the collection space is exhausted, and then insert the element will cover most of the initial head Elements!


Create a fixed set

Let's create a fixed set by createCollection and capped option is set to true:

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

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

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

Determine whether the collection of fixed collection:

>db.cappedLogCollection.isCapped()

If you need to convert an existing collection of fixed collection you can use the following command:

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

The code above our existing collection of posts converted to a fixed set.


Fixed collection query

Fixed set of documents stored in accordance with the insertion order, by default the query is inserted in accordance with the order to return, it can also be used to adjust the return $ natural order.

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

Fixed set of features

You can insert and update, but the update can not exceed the size of the collection, or the update fails, can not be deleted, but you can call drop () to delete all the rows in the collection, but after a drop to explicitly rebuild the collection.

Max on a 32-bit machine cappped collection of about 482.5M, 64-position is limited only by the size of the file system.


Fixed set of attributes and usage

Attributes

  • Attribute 1: insertion of a fixed set fast
  • Attribute 2: According to the query output speed is extremely fast insertion order
  • Attribute 3: able to insert the latest data, eliminating the oldest data

usage

  • Usage 1: Storing Log Information
  • Usage 2: a small cache of some documents