Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb create Collection Command db.createCollection details

Shulou Source: shulou.com Published: 2022-06-01 21:11:15 10月31日 Update

Mongodb create Collection Command db.createCollection details

The complete command is as follows:

Db.createCollection (name, {capped:, autoIndexId:, size:, max})

Name: the name of the collection

Capped: whether to enable collection restrictions. If a restriction is required to enable it, it is not enabled by default.

Size: limits the size of the space used by the collection. There is no limit by default.

Max: the maximum number of entries in the collection. There is no limit by default.

AutoIndexId: whether to use _ id as the index. Default is to use (true or false)

Size has a higher priority than max.

Introduction of common methods:

1. Generally speaking, we create a collection db.createCollection (name), such as db.createCollection ("log"), create a collection named log, without any size or quantity limit, and use _ id as the default index

2. Limit the size of the collection space: db.createCollection ("log", {capped:true,size:1024}). This must use capped:true, otherwise it will not take effect. Create a collection named log and limit its space size to 1m. If the size exceeds 1m, the oldest record will be deleted.

3. Limit the maximum number of entries in the collection: db.createCollection ("log", {max:1024}). Create a collection with the name log, and the maximum number of entries is 1024. If you insert data over 1024, the earliest record will be deleted. You can't use capped:true for this, otherwise an error will be reported.

4. Limit the maximum number of entries. Limit the size of the usage space: db.createCollection ("log", {capped:true,size:1024,max:1024}), limit the maximum usage space of the collection to 1m, and limit the maximum number of entries to 1024.

Tags: Limit maximum size space name command index generally speaking priority commonly used data quantity method condition will be reported Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi MariaDB Linux Shulou Tech Info