3.11. MongoDB creates a collection

发布时间 : 2025-10-25 13:34:24 UTC      

Page Views: 9 views

In this section, we show you how to use MongoDB to create collections.

Used in MongoDB createCollection() Method to create a collection.

Syntax format:

db.createCollection(name, options) 

Parameter description:

  • Name: the name of the collection to be created

  • Options: optional parameter that specifies options for memory size and indexing

Options can be the following parameters:

Field

Types

Description

capped

Boolean

(optional) if true, a fixed collection is created. A fixed set is a collection of fixed size that automatically overwrites the earliest documents when the maximum is reached.

When the value is true, the size parameter must be specified.

autoIndexId

Boolean

This parameter is no longer supported after 3.2. (optional) if true, automatically create an index in the _ id field. The default is false.

size

Numerical value

(optional) specify a maximum value, the number of bytes, for the fixed collection.

If capped is true, you also need to specify this field.

max

Numerical value

(optional) specifies the maximum number of documents contained in a fixed collection.

When inserting a document, MongoDB first checks the fixed collection’s size Field, and then check max Field.

3.11.1. Example

Create a runoob collection in the test database:

> use test switched to db test > db.createCollection("runoob") { "ok" : 1 } > 

If you want to view an existing collection, you can use the show collections or show tables command:

> show collections runoob system.indexes 

Here is the use of createCollection () with several key parameters:

Create a fixed collection mycol with a total space size of 6142800 B and a maximum of 10000 documents.

> db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) { "ok" : 1 } > 

In MongoDB, you don’t need to create a collection. When you insert some documents, MongoDB automatically creates collections.

> db.mycol2.insert({"name" : "菜鸟教程"}) > show collections mycol2 ... 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。