The MongoDB fixed set (Capped Collections) is a well-performing and fixed-size set, and for a fixed size, we can imagine it is like a ring queue, when the collection space is exhausted, The element that is inserted will overwrite the initial element of the head!
1. Create a fixed collection:
Db.createcollection ("Cappedlogcollection", {capped:true,size:10000})
Specify the number of documents:
Db.createcollection ("Cappedlogcollection", {capped:true,size:10000,max:1000})
Determines whether the collection is a fixed set:
Db.cappedLogCollection.isCapped ()
To convert a collection that already exists to a fixed collection:
Db.runcommand ({"converttocapped": "Posts", size:10000})
2. Fixed set query:
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 were inserted, or the order of return can be adjusted using $natural :
Db.cappedLogCollection.find (). Sort ({$natural:-1})
3. Features of the fixed set:
can insert and update But the update cannot exceed collection size Span style= "FONT-FAMILY:CALIBRI;" >, , But you can call drop () Delete all rows in the collection but drop After you need to explicitly rebuild the collection.
The maximum value of the previous cappped collection on the three-bit machine is only limited by the system file size on the 482.5m,64 bit.
4. Properties and usage of fixed sets:
Property:
(1) very fast insertion speed for fixed set
(2) The query output speed is very fast according to the insertion order.
(3) The ability to eliminate the earliest data when inserting the latest data
Usage:
(1) store log information
(2) cache Some small amount of documents
MongoDB fixed set (Capped collections)