Get the App
SLTechnology News&Howtos  ›  Database  › 

[MongoDB Learning Notes 26] A fixed set of MongoDB

Shulou Source: shulou.com Published: 2022-06-01 18:00:47 10月11日 Update

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

Tags: Document cursor index loop sort limit normal size field special byte time result natural order dynamic that is case manual data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Shulou Technology Apple Shulou Tech Info