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 create Collection Command db.createCollection details

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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.

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