MongoDB Create Collection
In this chapter we introduce how to use MongoDB to create collections.
In MongoDB, use createCollection() method to create a collection.
Syntax format:
db.createCollection(name, options)
Parameter Description:
- name: The name of the collection to create.
- options: Optional parameters specifying options about memory size and indexes.
options can be the following parameters:
| Parameter name | Type | Description | Example value |
|---|---|---|---|
capped |
Boolean | Whether to create a fixed-size collection. | true |
size |
Value | The maximum size of the collection (in bytes). Only valid when capped is true. |
10485760 (10MB) |
max |
Value | The maximum number of documents allowed in the collection. Only valid when capped is true. |
5000 |
validator |
object | An expression for document validation. | { $jsonSchema: { ... }} |
validationLevel |
String | Specify the strictness of document validation. |
`"off"`:不进行验证。
`"strict"`:插入和更新操作都必须通过验证(默认)。
`"moderate"`:仅现有文档更新时必须通过验证,插入新文档时不需要。 | `"strict"` |
| validationAction | string | Specify the action when document validation fails.
"error": Block insertion or update (default).
"warn": Allow insertion or update, but issue a warning. | "error" | | storageEngine | object | Specify the storage engine configuration for the collection. | { wiredTiger: { ... }} | | collation | object | Specify the default collation for the collection. | { locale: "en", strength: 2 } |
When inserting documents, MongoDB first checks the size field of the capped collection, then the max field.
An example of creating a collection using these options:
Example
db.
createCollection
(
"myComplexCollection"
,
{
capped
:
true
,
size
:
10485760
,
max
:
5000
,
validator
:
{
$jsonSchema
:
{
bsonType
:
"object"
,
required
:
[
"name"
,
"email"
]
,
properties
:
{
name
:
{
bsonType
:
"string"
,
description
:
"must be a string and is required"
}
,
:
{
bsonType
:
"string"
,
pattern
:
"^.+@.+$"
,
description
:
"must be a valid email address"
}
}
}
}
,
validationLevel
:
"strict"
,
validationAction
:
"error"
,
storageEngine
:
{
wiredTiger
:
{
configString
:
"block_compressor=zstd"
}
}
,
collation
:
{
locale
:
"en"
,
strength
:
2
}
}
)
;
This example creates a collection with the following features:
- Fixed size, maximum 10MB, storing at most 5000 documents.
- Documents must contain the
nameandemailfield, wherenamemust be a string,emailmust be a valid email format. - The validation level is strict, and validation failure will block insertion or update.
- Use the WiredTiger storage engine, specifying the block compressor as zstd.
- Use the English collation by default.
Example
Create the runoob collection in the test database:
> use test
switched to db test
> db.createCollection("runoob")
{ "ok" : 1 }
>
To view existing collections, you can use the show collections or show tables command:
> show collections
runoob
system.indexes
Below is the usage of createCollection() with several key parameters:
Created a fixed-size collection with a maximum size of 5MB (5242880 bytes), storing at most 5000 documents.
db.createCollection("myCappedCollection", { capped: true, size: 5242880, max: 5000 });
In MongoDB, you do not need to create a collection; when you insert some documents, MongoDB will automatically create the collection.
> db.mycol2.insert({"name" : "Runoob Tutorial"})
> show collections
mycol2
...
Created a fixed-size collection with a maximum size of 1MB (1048576 bytes):
db.createCollection("myCappedCollection", { capped: true, size: 1048576 });
The following example creates a validator for the myCollection collection, requiring documents to have name and age fields, where name must be a string and age must be a non-negative integer.
Example
db.
createCollection
(
"myCollection"
,
{
validator
:
{
$jsonSchema
:
{
bsonType
:
"object"
,
required
:
[
"name"
,
"age"
]
,
properties
:
{
name
:
{
bsonType
:
"string"
,
description
:
"must be a string and is required"
}
,
age
:
{
bsonType
:
"int"
,
minimum
:
0
,
description
:
"must be an integer and is required"
}
}
}
}
}
)
;