MongoDB fixed set of detailed

Source: Internet
Author: User
In general, we create a collection is no size, can be added to the inside of the document, the collection can be dynamically grown, there is a collection of MongoDB is called a fixed set, the size of the collection is fixed, I can be created when the set of the number of documents in the set, assumed to be 100, When the number of documents in the collection reaches 100, if you then insert a document into the collection, only the most recent 100 documents are retained, and the previous document is deleted. Generally like log information we can use a fixed set, and some other data that needs to be deleted periodically can also use a fixed set, this article we will look at the use of this fixed set.

This article will share with you the MongoDB fixed set of detailed.

Installing MongoDB on 1.Linux

2.MongoDB Basic Operation
3.MongoDB Data types
4.MongoDB Document Update operations
5.MongoDB Document Query operation (i)
6.MongoDB Document query operation (ii)
7.MongoDB Document query operation (iii)
8.MongoDB View Execution Plan
9. Initial knowledge of the index in MongoDB
Various types of indexes in 10.MongoDB

Create

The method of creating a fixed collection is also relatively simple, as follows:

Db.createcollection ("My_collect", {capped:true,size:10000,max:100})

The Capped:true parameter indicates that the collection is a fixed-size collection, which represents the size of the collection, and in Kb,max represents the maximum number of documents in the collection. We are here to give a fixed set of two restrictions, as long as any one of the constraints met, the collection will begin to delete more ancient data. Fixed set once created can no longer be modified, want to modify can only delete again. At this point we can try to add 120 simple data to the collection, and then we'll see that the first 20 data disappears.

In addition to creating a fixed set directly, we can also convert a normal collection to a fixed set by converttocapped operation, as follows:

Db.runcommand ({converttocapped: "Sang_collect", size:10})

Natural sorting problems

Natural sorting is arranged according to the order of the documents in the disk, and it doesn't make much sense to sort naturally in a normal collection, because the position of the document is always changing, and the documents in the fixed collection are saved in the order in which the documents are inserted, and the natural order is the order in which the documents are inserted. So we can use natural sort to sort documents from old to new, as follows:

Db.sang_collect.find (). Sort ({$natural: 1})

You can also sort from new to old:

Db.sang_collect.find (). Sort ({$natural:-1})

Other operations in a fixed set are basically consistent with normal collections, which are not described here.

The above content is MongoDB fixed set of detailed, hope to help everyone.

MongoDB Tips and Considerations Summary

Laravel How to use MongoDB database tutorial

How to improve the security of MongoDB

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.