A fixed set of MongoDB

Source: Internet
Author: User

  First, MongoDB fixed set concept

    A fixed set refers to a set that is created beforehand and fixed in size. That is, if a collection is set to a fixed size of 100, and then a document is added, the first document will be removed, leaving only 100 data.

Fixed set properties: Fixed collections are like ring queues, and if there is not enough space, the earliest documents are deleted, freeing up space for new documents. In general, fixed collections are suitable for any scenario where you want to automatically retire expired attributes.

Second, fixed set of application scenarios

    such as log files, chat records, call information records, and so on only to retain the most recent period of time in the application scenario, will use the fixed set of MongoDB.

Iii. How to use a fixed set

Create a fixed collection using the command: db.createcollection ("set noun", {capped:true,size:1000,max:100});

Capped:true indicates that the collection is a fixed collection, and the size specifies the collection sizes, in Kb,max the number of documents specified

    When you specify a limit on the number of documents, you must specify both size. What is this for?

The answer is: the elimination mechanism will only work based on the number of documents when the capacity is not yet full. If the capacity is full, the elimination mechanism will work according to capacity.

   Combine scenarios to understand:

(1) Now create a fixed set of C5, size set to 1000KB, the number of documents set to 100, write command: db.createcollection ("C5", {capped:true,size:1000,max:100});

(2) and insert 10 data into the collection:

for (Var i=0,i<=99;i++) {
Db.c5.insert ({name: "Fuck", age:i})
}

(3) Find the size of the C5 collection: Db.c5.count (), but found that the actual number of displays is only 56.

Conclusion: When the stored data reaches the upper limit of the set size, the elimination mechanism is based on the capacity to work, through the continuous elimination of the previously inserted data to meet all the data inserted later, so according to available, C5 retrieved results, is from the age:43 start.

    

Look at a scene again.

(1) Now create a fixed set of C8, the size is set to 10000KB, the number of documents is set to 100, write the command: db.createcollection ("C8", {capped:true,size:10000,max:100}); The condition is similar to the previous scene, except that the size is adjusted to 10000KB

(2) and insert 100 data into the collection:

for (Var i=0,i<100;i++) {
Db.c8.insert ({name: "Fuck", age:i})
}

(3) Find the size of the C8 collection again: Db.c8.count (), this time the number reached the expected 100

(4) Then insert a data, can also be inserted successfully, but find again, will find that the first data was killed

Conclusion: The elimination mechanism in the case of the capacity is not full, the previous document will be eliminated, after inserting how many data, then the first corresponding elimination of how many bars.

A fixed set of MongoDB

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.