Capped Collection Fixed Set
Simple Introduction
Capped collections is a well-performing, fixed-size collection that performs used (aging-out) processing with LRU (Least recently age-out Least recently used) rules and insertion order, automatically maintaining the order in which objects in the collection are inserted, when created To specify the size beforehand. If the space is exhausted, the newly added object will replace the oldest element in the collection.
Keep up-to-date data forever.
Functional Features:
can be inserted and updated, but the update cannot exceed the size of the collection, otherwise the update is white. Delete is not allowed, but you can call drop () to delete all the rows in the collection, but the drop needs to be displayed to rebuild the collection.
The maximum value of a capped collection on a 32-bit machine is limited only by the size of the system file on the 482.5m,64 bit.
Properties and Usage
Attribute 1: Inserts the fixed set very quickly
Attribute 2: Query output in the order of insertion is extremely fast
Attribute 3: The oldest data can be retired when the latest data is inserted.
Usage 1: Store log information
Usage 2: Cache a few documents
Create a pinned Collection
Creating a pinned collection is not like a normal collection, and a fixed collection needs to be created explicitly using the CreateCollection command.
Create collections explicitly
> Show Dbsadmin (empty) dt1 0.078gbdt2 0.078gbdt3 0.078GBlocal 0.078gb> dbtest> db.createcollection ("C4 ") {" OK ": 1}> Show collectionsc4system.indexes> Db.c4.drop () true> show Dbsadmin (empty) dt1 0.078GBDT2 0. 078GBDT3 0.078GBlocal 0.078GBtest 0.078gb>
Show creating a pinned collection
Db.createcollection ("My_collction", {capped:true,size:10000})
Creates a fixed collection of "My_collction" with a size of 10000 bytes. You can also limit the number of documents. Plus the Max:100 property.
Note: Specify the upper limit of the document, you must specify the size. When the document is limited, it is retired when the capacity is not full, and if it is full, it is retired according to capacity.
> use dt4switched to db Dt4> dbdt4> show collections> > db.c1.insert ({User: "Happy", Gender: "Male"} ) Writeresult ({ "ninserted"  : 1 }) > > show collectionsc1system.indexes > db.c1.stats () {"ns" : "Dt4.c1", "Count" : 1, "size" : 112, "Avgobjsize" : 112, "Storagesize" : 8192, "numextents" : 1, "nindexes" : 1, " Lastextentsize " : 8192," Paddingfactor " : 1," Systemflags " : 1," UserFlags " : 1, "Totalindexsize" : 8176, "indexsizes" : {"_id_" : 8176}, "OK" : 1}> db.system.indexes.find () { "V" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "Dt4.c1"  }>
Now create a fixed collection of C2. The Set collection size is 10000000 bytes, approximately 10M, and the document is capped at 5.
View the status of the C2 discovery, except that the namespace NS has changed. Storagesize, Max, capped several parameters are also worth noting.
> db.createcollection ("C2", {capped:true,size:10000000,max:5}) {"OK": 1}> show tablesc1c2system.indexes> Db.c2.stats () {"ns": "Dt4.c2", "Count": 0, "size": 0, "storagesize": 10002432, "numextents": 1, "nindexes": 1, "lastextents Ize ": 10002432," Paddingfactor ": 1," systemflags ": 1," UserFlags ": 0," totalindexsize ": 8176," indexsizes ": {" _id_ ": 817 6}, "capped": True, "Max": 5, "OK": 1}>
So, let's now insert the document data into the C2 to see what happens.
> db.c2.insert ({name: "user1"}) Writeresult ({ "ninserted"  : 1 }) > Db.c2.insert ({name: "User2"}) Writeresult ({ "ninserted"  : 1 }) > db.c2.insert ({name: " User3 "}) Writeresult ({ " ninserted " : 1 }) > db.c2.insert ({name:" User4 "}) Writeresult ( { "ninserted"  : 1 }) > db.c2.insert ({name: "User5"}) Writeresult ({ "ninserted"  : 1 }) > db.c2.find () { "_id" : objectid ("55069B2AC1728741D70B54FA"), "name" : "user1" }{ "_id" : objectid ("55069B32C1728741D70B54FB"), "name" : "User2" }{ "_id" : objectid ("55069B38C1728741D70B54FC"), "name" : "User3" }{ "_id"  : objectid ("55069B3DC1728741D70B54FD"), "name" : "User4" }{ "_id" : ObjectId ("55069b42c1728741d70b54fe"), "name" : "User5" }> db.c2.insert ({Name: "User6"}) Writeresult ({ "ninserted"  : 1 }) > db.c2.find () { "_id" : ObjectId (" 55069B32C1728741D70B54FB "), " name " : " User2 " }{ " _id " : objectid (" 55069B38C1728741D70B54FC "), " name " : " User3 " }{ " _id " : objectid (" 55069B3DC1728741D70B54FD "), " name " : " User4 " }{ " _id " : objectid (" 55069b42c1728741d70b54fe "), " name " : " User5 " }{ " _id " : objectid (" 55069b50c1728741d70b54ff "), " name " : " User6 " }>
So can we turn our common set into a fixed set? The answer is yes.
Convert Collection
Convert a common set into a fixed set
Need to use the converttocapped command
Db.runcommand ({converttocapped: "C1", size:10000})
Converts a C1 normal collection to a fixed set with a size of 10000 bytes.
Natural sort
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.my_collections.find (). Sort ({"$natrual": 1})
1 indicates the default order, and 1 is the opposite.
Here's a summary of two ways to determine whether a collection is a fixed set:
One is db.c2.isCapped ()
The other is Db.c2.stats ()
Let's take a practical look at how to convert a normal collection to a fixed set.
> db.c1.isCapped () false> Db.runcommand ({converttocapped: "C1", Size:10000000,max:3}) {"OK": 1}> db.c1.isCapped () true> db.c1.stats () {"ns": "Dt4.c1", "Count": 1, "Size": "," "avgobjsize":----"storagesize": 10002432 , "numextents": 1, "nindexes": 1, "lastextentsize": 10002432, "Paddingfactor": 1, "systemflags": 1, "UserFlags": 0, "Totali Ndexsize ": 8176," indexsizes ": {" _id_ ": 8176}," capped ": True," Max ": Numberlong (" 9223372036854775807 ")," OK ": 1}>
MongoDB Advanced Series (--MONGODB) fixed set