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 Learning Notes 26] A fixed set of MongoDB

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Ordinary sets in MongoDB are dynamic and can automatically grow to accommodate more data; relatively speaking, there is another set called fixed set.

(1) A fixed collection needs to be explicitly created, and the size of the collection is fixed.

(2) once the space in the fixed set is used up, the inserted new document will cover the space of the oldest document, similar to a circular queue.

(3) fixed sets cannot be segmented.

(4) fixed sets cannot be modified and can only be deleted and rebuilt.

First, create a fixed collection

Use CreateCollection to create a fixed collection myCollection with a byte size limit of 10000 and a document number limit of 100:

> db.createCollection ("myCollection", {capped:true,size:10000,max:100}) {"ok": 1} >

Capped ": whether to fix the collection

"size": specifies the byte size of a fixed collection

"max": specifies document limits in a fixed collection

Whether it is the first-to-size restriction or the max restriction, new documents overwrite the oldest documents

Second, convert an ordinary set into a fixed set

Use convertToCapped to convert an existing collection to a fixed collection:

> db.runCommand ({convertToCapped: "foo", "size": 10000}) {"ok": 1} >

III. Natural ordering of fixed sets

A fixed set has a special quota sort, called natural sort, which returns documents in the result set in the order in which they are on disk.

The default document is sorted from old to new, or you can specify the sort of document from new to old:

> db.foo.find () .sort ({$natural:-1}) {"_ id": 3, "x": 3} {"_ id": 2, "x": 2} {"_ id": 1, "x": 1} {"_ id": 0} >

4. Collections without _ id index

By default:

(1) ordinary collections have an "_ id" index. Specifying autoIndexId as false when explicitly creating a collection will not automatically create an index on the _ id field

(2) A fixed collection does not have an index for the _ id field, unless autoIndexId is explicitly specified as true at creation time, or manually created with ensureIndex afterwards.

(3) the index on the _ id field must be unique.

Circular Vernier

A special cursor that can only be in a fixed set.

(1) when the result set in the cursor is taken out, the cursor will not be closed, but will be placed in the cursor after a new document is inserted into the collection.

(2) Loop cursors are automatically closed after 10 minutes of timeout. If you want to use loop cursors, you also need to execute the query automatically after the cursor is released.

(3) Loop cursors cannot be used in MongoDB shell

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

Database

Wechat

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

12
Report