Create a fixed collection
A bit similar to the circular queue in the data structure , first in first out. When the queue is full, delete the original element.
MongoDB Capped Collections are collections with excellent performance and a fixed size. For a fixed size, we can imagine that it is like a circular queue. When the collection space is used up, the inserted elements will cover the initial header. Elements!
Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations. By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.
Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.
A brief introduction to Capped Collection:
Cappend collection is a collection of fixed size with excellent performance. It performs age-out (aging out) processing according to LUR (Least Recently Used) rules and insertion order, and automatically maintains the insertion order of objects in the collection. Specify the size, if the space runs out, the newly added object will replace the oldest object in the collection, always keeping the latest data
Features:
You can insert and update, but the update cannot exceed the size of the collection, otherwise the update fails, and deletion is not allowed, but you can call drop() to delete all the rows in the collection, but the reconstruction collection needs to be displayed after drop, currently a capped in 32-bit machines The maximum value of collection is about 482.5M, which is only limited by the system file size on 64-bit
Properties and usage:
Property 1: Inserting a fixed collection is faster
Attribute 2: The query speed according to the insertion order is faster
Attribute 3: Ability to eliminate the oldest data when inserting the latest data
Usage 1: Store log information
Usage 2: Cache some small amount of documents
Create a fixed collection
Unlike normal collections, fixed collections need to be explicitly created using
createCollection command to create
db.createCollection('my_collection', {capped:true, size:10000});
Create a fixed collection with the collection "my_collection", the size is 10000 bytes, you can also limit the number of documents, plus the Max 100 attribute
Note: The specified upper limit of the document must be specified in size. The document limit is to be eliminated when the capacity is not full. If it is full, it will be eliminated according to the capacity limit.
db.c1.stats(); //View the details of the set, whether capped is true means it is a fixed set
Convert to Fixed Collection
Ordinary collections can be converted to fixed collections through convertToCapped
db.runCommand({convertToCapped:”test_2”,size:10,max:3})
But the characteristics of fixed sets are:
Partitioning cannot be performed, because it is not for large data sets, so maybe TTL indexes can be considered at this time.