MongoDB Combat (3) fixed set and Gridfs

Source: Internet
Author: User
Tags file system mongodb

One, fixed set (Capped Collection)

The capped collections is an excellent set of fixed sizes, recently (aging out) processing with LRU (least Used age-out least used) rule and insertion order, automatically maintaining the insertion order of objects in the collection. Specify the size in advance when you create it. If the space is exhausted, the newly added object will replace the oldest object in the collection.

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

Common uses:

1, logging

MongoDB is the first choice for logging mechanisms, MongoDB does not use log files, but instead stores log events in the database. Inserting objects in an capped collection without an index is as fast as logging in the file system.

2, Cache

Caches some objects in the database, such as computed statistics. Such a need is to build an index on the collection, because caching is often more read than written.

3, Auto archiving

The Age-out feature of capped collection can be used to eliminate the job of writing cron scripts for manual archiving.

Recommended usage:

1, in order to play the maximum performance of capped collection, if write more than read, it is best not to build the index, or insert speed from "log speed" down to "database speed."

2, the use of "nature ordering" can effectively retrieve the most recently inserted elements, because capped collection can ensure that the natural sort is inserted in the order, similar to the log file on the tail operation.

Actual case:

You can specify the maximum number of documents that can be stored in collection when you create capped collection. However, you also specify size because you always check the size before checking maxrownumber.

You can use validate () to see how much space a collection has already used to determine how big size is set.

Here we create a collection:

Db.createcollection (' Mycappc1 ', {capped:true,size:100000,max:10})

This is a fixed collection of up to 10 rows of records.

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.