[MongoDB] The Capped Collection set of Mongodb databases, mongodbcapped
Capped Collection is a set of excellent performance with a fixed size. It executes age-out (aging removal) processing in the LRU (least Recently Used, least Recently Used) rule and insertion sequence, the insertion sequence of objects in the set is automatically maintained.
1. Create a Capped Collection
The size must be specified in advance during creation. If the space is used up, the newly added object will replace the most recent object in the collection. If the size of the colleond ond is exceeded, the update fails. Although deletion is not allowed, you can call the drop method to delete all documents in the set. After deletion, the rebuild set is displayed. On 32 machines, the maximum value of a capped collection is about 482.5 MB, and 64-bit is only limited by the size of the system file.
To convert a common collection to a Capped Collection, you only need to call the convertToCapped () command;
Ii. Usage of Capped Collection
Capped Collection is the preferred Log Mechanism in mongodb. Instead of using log files, mongodb stores log files in the database. The insert speed of objects in a capped collection without indexes is the same as that recorded in the file system.
Some objects are cached in the memory. For example, you need to create an index on the collection for the calculated statistical information, because the cache is often used to read more than write.
You can use the age-out feature of capped collection to save the effort of writing a crontab script to perform manual archiving.
Iii. Notes
1. If there are more writes than reads, it is best not to create an index on it;
2. Using natual ordering can effectively retrieve Recently inserted elements, because the capped collection ensures that the natural sorting is the insertion order.
3. capped collection cannot be shard.
4. You can specify the maximum number of documents that can be stored in the collection when creating a capped collection.