3.11. MongoDB creates a collection

发布时间 :2025-10-25 12:33:01 UTC      

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
...
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.