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