MongoDB Finishing Note のcapped Collection

Source: Internet
Author: User

1. Brief 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, You specify the size beforehand when you create it. If the space is exhausted, the newly added object will replace the oldest object in the collection.

2. Functional characteristics

can be inserted and updated, but the update cannot exceed the size of the collection, or the update fails. Delete is not allowed, but you can call drop () to delete all rows in the collection, but you need to explicitly rebuild the collection after the drop. On a 32-bit machine, the maximum value of a capped collection is only limited by the size of the system file on the 482.5m,64 bit.

3. Common use
(1) Logging

The preferred logging mechanism in MongoDB, MongoDB does not use log files, but instead stores log events in the database. Inserting an object in a capped collection without an index is equivalent to the speed at which the log is recorded in the file system.

(2) Cache

Caches some objects in the database, such as calculated statistics. This requires an index on the collection, because using the cache tends to be more read than write.

(3) Auto archiving

The Age-out feature of the capped collection can be exploited, eliminating the task of writing cron scripts for manual archiving.

4. Recommended usage
(1) In order to play the maximum performance of capped collection, if write more than read, it is best not to index on the above, otherwise the insertion speed from "log" to the "database".

(2) using "nature ordering" can effectively retrieve the most recently inserted element, because capped collection can guarantee that the natural sort is the order of insertion, similar to the tail operation on the log file.

5. Precautions

(1) You can specify the maximum number of documents that can be held in collection when creating capped collection. However, you should also specify size, because the size is always checked before checking for Maxrownumber. You can use validate () to see how much space a collection has used to determine how large the size is. Such as:
Db.createcollection ("Mycoll", {capped:true, size:100000, max:100});

Db.mycoll.validate (); Max=1 will store as many documents as possible in collection.

(2) The CreateCollection function above can also be used to create a general collection, there is a parameter "Autoindexid", the value can be "true" and "false" to determine whether you need to automatically create an index on the "_id" field, such as:
Db.createcollection ("Mycoll", {size:10000000, autoindexid:false}).

By default, the generic collection is indexed, but not created for capped collection.

6. Case operation

MongoDB supports Capped Collection, a fixed-size collection where the new data overwrites the old data when the size of the collection reaches the specified size, and the Oplog in MongoDB Replica set is the Capped Collection type.
--1 See if Oplog is Capped Collection

[Email protected] ~]$ MONGO 127.0.0.1:27018   2.2.1   127.0.0.1:27018/test   Rs0: PRIMARY> Use local;   Switched to DB local   rs0:primary> show Collections;   Me   oplog.rs   replset.minvalid   slaves   system.indexes   system.replset   rs0:primary > db.oplog.rs.isCapped ();    true

Note: the db.collection.isCapped () command lets you see if a collection is Capped collection.
Capped Collection has the following characteristics, which need to be noted when used:
1 The Capped Collection can not be fragmented.
2 after the 2.2 version, the capped Collection created by default creates an index on the _id field, but not in the 2.2 version or previously.
3 Capped Collection can be updated after inserting a document, when the update does not cause the original document to occupy
The space grows, or the update fails.
4 You can not perform a delete document operation on capped collection, but you can delete the entire collection.
Some of these features are then tested.

--2 Creating Capped Collection

  Rs0:primary> db.createcollection ("Mycoll1", {capped:true, size:1024});     OK ": 1}

Note: The Capped Collection Collection is created with the db.createcollection command and must be created with the collection size specified for pre-allocating space.

--3 See if a collection is Capped Collection
There are two ways to see if a collection is Capped Collection.

Rs0:primary>db.mycoll1.isCapped (); truers0:primary>db.mycoll1.stats (); {        "NS": "Test.mycoll1",        "Count": 0,        "Size": 0,        "Storagesize": 4096,        "Numextents": 1,        "Nindexes": 1,        "Lastextentsize": 4096,        "Paddingfactor": 1,        "Systemflags": 1,        "UserFlags": 0,        "Totalindexsize": 8176,        "Indexsizes" : {                "_id_": 8176        },        "Capped":true,        "Max": 2147483647,        "OK": 1    }

Note: The "capped" property is true to indicate that it is capped Collection.

--4 test: Inserting records

Rs0:primary> for(var i = 1; I <= 10000; i++) Db.mycoll1.save ({id:i, Name: ' Francs '}); Rs0:primary>Db.mycoll1.find (). Count (); 56rs0:primary>Db.mycoll1.find (); { "_id": ObjectId ("50b811cf68b1911e7096db7f"), "id": 9945, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db80"), "id": 9946, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db81"), "id": 9947, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db82"), "id": 9948, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db83"), "id": 9949, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db84"), "id": 9950, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db85"), "id": 9951, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db86"), "id": 9952, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db87"), "id": 9953, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db88"), "id": 9954, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db89"), "id": 9955, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8a"), "id": 9956, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8b"), "id": 9957, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8c"), "id": 9958, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8d"), "id": 9959, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8e"), "id": 9960, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db8f"), "id": 9961, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db90"), "id": 9962, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db91"), "id": 9963, "name": "Francs" }   { "_id": ObjectId ("50b811cf68b1911e7096db92"), "id": 9964, "name": "Francs"} Type"It" forMore

Note: Because the size of the collection is limited, the target is inserted into 10,000, the result is only 56 data inserted, and the old data is overwritten by new data. In addition, you can not delete Capped Collection documents, under test.

--5 Test: Delete a document from the capped collection

Rs0:primary> Db.mycoll1.remove ({id:9956});   Canot Remove from a capped collection

Note: Throws an exception when deleting a document.

--6 testing: Updating documents in capped collection

Rs0:primary> Db.mycoll1.find ({id:9956});   " _id ": ObjectId (" 50b811cf68b1911e7096db8a ")," id ": 9956," name ":" Francs " }   rs0:primary> Db.mycoll1.update ({id:9956},{$set: {name: ' Aaa_francs '}});   Failing update:objects in a capped NS cannot grow   rs0:primary> Db.mycoll1.update ({id:9956},{$set: {name: ' BBB '}});   Rs0:primary> Db.mycoll1.find ({id:9956});   " _id ": ObjectId (" 50b811cf68b1911e7096db8a ")," id ": 9956," name ":" BBB "}

Note: This validates the feature 3, the updated value cannot exceed the original space, or the update fails.

--7 website Reference
http://docs.mongodb.org/manual/core/capped-collections/

MongoDB Finishing Note のcapped Collection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.