Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB fixed set (capped collection)

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

One. What is a fixed set?

There is a special type of collection in MongoDB that deserves our special attention: fixed collections (capped collection).

Fixed collections can declare the capacity size of a collection and behave like a circular queue. When the data is inserted, the new document is inserted at the end of the queue, and if the queue is already full, the oldest document is overwritten by the document inserted later.

Fixed set feature: fixed sets are much like circular queues, where if there is not enough space, the earliest documents are deleted to make room for new documents. In general, fixed collections are suitable for any scenario where you want to automatically eliminate expired attributes.

Fixed set application scenario

For example, log files, chat records, call information records, etc. as long as you keep the application scenarios in a certain period of time, you will use a fixed collection of MongoDB.

Advantages of fixed sets

1. Write speed increased. The data in the fixed collection is sequentially written to the fixed space on disk, so it will not be "interrupted" by some random write operations of other collections, and its write speed is very fast (better performance without indexing).

two。 Fixed collections automatically overwrite the oldest documents, so there is no need to configure additional work to delete old documents. Setting Job for regular deletion of old documents is easy to form performance pressure burrs.

Fixed collections are very useful in scenarios such as logging.

Two. Creation of fixed sets

Unlike normal collections, fixed collections must be explicitly created before use.

For example, create a fixed collection coll_testcapped with a size limit of 1024 bytes.

Db.createCollection ("coll_testcapped", {capped:true,size:1024})

In addition to the size, you can also specify the amount of data for documents in a fixed collection at creation time.

For example, create a fixed collection coll_testcapped with a size limit of 1024 bytes and a document count limit of 100.

Db.createCollection ("coll_testcapped2", {capped:true,size:1024,max:100})

Another way to create a fixed collection is to replace a normal collection with a fixed collection, using the command convertToCapped.

For example, convert a testcol1 collection to a fixed collection of 1024 bytes:

Db.runCommand ({"convertToCapped": "testcol1", "size": 1024})

Three. View of fixed collection information

(1) to determine whether a set is a fixed set, the decision command is:

Db. Collection .isCapped ().

For example, determine whether the previously created fixed set coll_testcapped2 is a fixed set:

(2) to obtain properties about a fixed collection from the collection information, the instructions for viewing the collection are:

Db. Collection .stats ()

For example, view the information of the collection coll_testcapped2:

Four. Note:

(1) after the fixed collection is created, it can not be changed, but can only be deleted and rebuilt.

(2) ordinary collections can be fixed using convertToCapped transformations, but fixed collections cannot be converted to normal collections.

(3) when you create a fixed collection and specify a limit on the number of documents for the fixed set (parameter max), you must also specify the size of the fixed set (parameter size). No matter which limit is reached first, the new document inserted later removes the collection of the oldest documents.

(4) when you use the convertToCapped command to convert an ordinary collection into a fixed collection, the existing index will be lost and need to be created manually. Also, this conversion command does not have parameters that limit the number of documents (that is, there is no parameter option for max).

(5) fixed sets cannot be segmented.

(6) the documents in the fixed collection can be update, but the update can not cause the Size of the document to grow or shrink, otherwise the update fails.

If there is a key in the collection, its value corresponds to a data length of 100bytes, and if you want to update the value corresponding to this key, the updated value must also be 100bytes, neither greater than 100bytes nor less than 100bytes.

The error message is: Cannot change the size of a document in a capped collection: XXXX (XXXX represents a data word)! = XXXX.

(7) you cannot delete a document on a fixed collection, but you can delete the entire collection.

When deleting a document, the error message is: cannot remove from a capped collection:XXXX

(8) it is also important to note that when estimating the size of a set, it is not based on the storageSize of the set, but on the size of the set. StorageSize is compressed by wiredTiger storage engine using high compression algorithm.

For example, through db. The collection .stats () command looks at the data of a collection, and there is still a big difference between "size" and "storageSize".

Articles that you may be interested in:

MongoDB tutorials for copying databases and collections between different hosts

Explain in detail the operation methods of creating and deleting collections in MongoDB

Simultaneous release of articles: https://www.geek-share.com/detail/2756840364.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report