MongoDB fixed set (Capped Collections) is a collection with excellent performance and fixed size. For a fixed size, we can imagine that it is like a circular queue. When the set space is used up, the inserted element will cover the original header element! We create a fixed collection through createCollection, and the capped option is set to true: You can also specify the number of documents, plus the max:1000 attribute: Determine whether the set is a fixed set: If you need to convert an existing collection to a fixed collection, you can use the following command: The above code converts our existing posts collection into a fixed collection. Fixed collection documents are stored in insertion order, and queries are returned in insertion order by default, or you can use $natural to adjust the return order. 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 rows in the collection, but you need to explicitly rebuild the collection after drop. The maximum value of a cappped collection on a 32-bit machine is about 482.5m, which is limited only by the size of the system file on 64-bit. Attribute 1: inserting a fixed collection is extremely fast Attribute 2: query output in insertion order is extremely fast Attribute 3: the ability to eliminate the earliest data when inserting the latest data Usage 1: store log information Usage 2: cache a small number of documents 3.44.1. Create a fixed collection ¶
>db.createCollection("cappedLogCollection",{capped:true,size:10000})
>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
>db.cappedLogCollection.isCapped()
>db.runCommand({"convertToCapped":"posts",size:10000})
3.44.2. Fixed set query ¶
>db.cappedLogCollection.find().sort({$natural:-1})
3.44.3. The functional characteristics of fixed sets ¶
3.44.4. Fixed set attributes and usage ¶
Attribute ¶
Usage ¶