MongoDB Study Notes (4)-capped collection

Source: Internet
Author: User

Brief Introduction

Capped collections is a set of excellent performance with a fixed size (Set of capacity), with LRU (least recently used) Rules and Insertion Sequence for age-out (aging removal) processing: automatically maintains the insertion sequence of objects in the set. You must specify the size in advance when creating the set. If the space is used up, the newly added object will replace the oldest object in the set.

Features 

It can be inserted and updated, but the update cannot exceed the collection size. Otherwise, the update fails. It is not allowed to be deleted. However, you can call drop () to delete all rows in the set. However, after dropping, you must explicitly recreate the set. On a 32-bit host, the maximum value of a capped collection is about 482.5 MB, and the 64-bit host is only limited by the size of the system file.

Common usage

1. Logging

MongoDB is the preferred Log Mechanism. Instead of using log files, MongoDB stores log events in the data base. The insert speed of objects in a capped collection without indexes is equivalent to the speed at which logs are recorded in the file system.

2. Cache

Cache some objects in the database, such as the calculated statistical information. In this case, you need to create an index on the collection, because the cache is usually more read than write.

3. Auto Archiving

The age-out feature of capped collection can be used to save the effort of writing cron scripts for manual archiving.

Recommended usage

1. In order to maximize the performance of the capped collection, if there are more writes than reads, it is best not to create an index on it; otherwise, the insertion speed is reduced from "log speed" to "database speed ".

2. The use of "Nature ordering" can effectively retrieve Recently inserted elements, because the capped collection ensures that the natural sorting is the order of insertion, similar to the tail operation on log files.

Notes

1. You can specify the maximum number of documents that can be stored in the collection when creating a capped collection. However, you also need to specify the size because you always check the size before checking maxrownumber. You can use validate () to check the space used by a collection, and then set the size to a large value.

DB. createcollection ("mycoll", {capped: True, size: 100000, Max: 100 });

DB. mycoll. Validate ();

When Max = 1, as many documents as possible will be stored in the collection.
2. the createcollection function can also be used to create a general collection, and the "autoindexid" parameter also exists ", the value can be "true" or "false" to determine whether to automatically create an index on the "_ id" field, such as DB. createcollection ("mycoll", {size: 10000000, autoindexid: false }). By default, indexes are created for General collections, but capped collections are not created.

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.