Introduction to Capped Collection
Capped Collection is a special Collection with a fixed size. When the Collection size reaches the specified size, new data overwrites old data. Capped collections can be saved to the collection according to the document insertion order, and these documents are stored on the disk in the insertion order. Therefore, when we update the documents in Capped collections, the size of the updated document cannot exceed the size of the previous document. In this way, you can ensure that the positions of all documents on the disk remain unchanged.
Because Capped collection is based on the document insertion order rather than using indexes to determine the Insertion Location, this can increase the efficiency of data. MongoDB's Operation Log File oplog. rs is implemented using Capped Collection.
In addition, Capped Collection has the following features: First, it cannot be deleted, but you can call drop () to delete all rows in the set, the reason for not deleting is to keep each document on disk unchanged. The maximum value of a capped collection on a 32-bit machine is about 482.5 MB, and the system file size is not limited on 64-bit machines. Capped Collection cannot be sharded. After Version 2.2, the created Capped Collection creates an index on the _ id field by default, but does not exist in version 2.2 or before.
Capped Collection is mainly used to store log information and cache infrequently used documents.
Use Capped Collection
1. Create Capped Collection
> Db. createCollection ("mycoll", {capped: true, size: 1024, max: 10 })
{"OK": 1}> for (var I = 1; I <1000; I ++) db. mycoll. save ({id: I, name: "Hello"})> db. mycoll. count () 10> db. mycoll. find () {"_ id": ObjectId ("5274b854f007ff8bb84f6347"), "id": 990, "name": "Hello"} {"_ id ": objectId ("5274b854f007ff8bb84f6348"), "id": 991, "name": "Hello"} {"_ id": ObjectId ("identifier"), "id": 992, "name": "Hello"} {"_ id": ObjectId ("5274b854f007ff8bb84f634a"), "id": 993, "name ": "Hello" }{ "_ id": ObjectId ("5274b854f007ff8bb84f634b"), "id": 994, "name": "Hello"} {"_ id ": objectId ("5274b854f007ff8bb84f634c"), "id": 995, "name": "Hello"} {"_ id": ObjectId ("identifier"), "id": 996, "name": "Hello"} {"_ id": ObjectId ("5274b854f007ff8bb84f634e"), "id": 997, "name ": "Hello" }{ "_ id": ObjectId ("5274b854f007ff8bb84f634f"), "id": 998, "name": "Hello"} {"_ id ": objectId ("5274b854f007ff8bb84f6350"), "id": 999, "name": "Hello "}
> Db. mycoll. isCapped () true> db. mycoll. stats () {"ns": "test. mycoll "," count ": 0," size ": 0," storageSize ": 4096," numExtents ": 1," nindexes ": 1," lastext size ": 4096, "paddingFactor": 1, "systemFlags": 1, "userFlags": 0, "totalIndexSize": 8176, "indexSizes": {"_ id _": 8176 }, "capped": true, "max": NumberLong ("9223372036854775807"), "OK": 1}
The above creates a Capped Collection, which occupies 1024 bytes and can contain up to 10 documents. Then, 1000 pieces of data are inserted to it through the database. collection. isCapped () and db. mycoll. the stats () command can be used to check whether a Collection is a Capped Collection.
Note: a. Creating a fixed set is not like creating a common set. You need to create and use a fixed set.
B. You must specify the size of a fixed set to be created. Otherwise, an error is returned:
> Db. createCollection ("mycoll", {capped: true })
{"Errmsg": "exception: specify size: <n> when capped is true", "code": 14832, "OK": 0}
C. The Max attribute is optional when a set is created. The document quantity limit is used out when the size is not full. If the size is full, data is replaced based on the capacity limit. For example, in the above example, we set the document quantity limit to 10. In this case, when the capacity limit is not reached, a maximum of 10 documents can be stored.
For more details, please continue to read the highlights on the next page:
MongoDB details: click here
MongoDB: click here
Recommended reading:
Java-based self-growth field in MongoDB
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
How to create a new database and set in MongoDB
MongoDB beginners must read (both concepts and practices)
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]