MongoDB Fixed Collection

Source: Internet
Author: User

The MongoDB fixed set (Capped collections) is a well-performing and fixed-size set, and for a fixed size we can imagine it to be like a ring queue, and when the set space is exhausted, then the element that is inserted will overwrite the element of the original head!

Create a pinned Collection

We create a fixed collection with CreateCollection, and the capped option is set to true:

>db.createcollection ("Cappedlogcollection", {capped:true,size:10000})

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

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

Determines whether the collection is a fixed set:

>db.cappedlogcollection.iscapped ()

If you need to convert a collection that already exists to a fixed collection, you can use the following command:

>db.runcommand ({"converttocapped": "Posts", size:10000})

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

Fixed collection queries

Fixed collection documents are stored in the order in which they are inserted, by default the query is returned in the order in which they are inserted, or the return order can be adjusted using $natural.

>db.cappedlogcollection.find (). Sort ({$natural:-1})
Functional characteristics of fixed sets

can be inserted and updated, but the update cannot exceed the size of the collection, otherwise the update fails, the deletion is not allowed, but the drop () can be called to delete all rows in the collection, but the collection needs to be explicitly rebuilt after the drop.

The maximum value for a cappped collection on a 32-bit machine is only limited by the system file size on the 482.5m,64 bit.

Fixed set properties and Usage properties
    • Attribute 1: Inserts the fixed set very quickly
    • Attribute 2: Query output in the order of insertion is extremely fast
    • Attribute 3: Ability to retire the oldest data when inserting the latest data
Usage
    • Usage 1: Store log information
    • Usage 2: Cache a few documents

MongoDB Fixed Collection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.