Introduction
A MongoDB collection serves as a container for related MongoDB documents, similar to a table in relational databases. Knowing how to construct and manage collections is an essential step when creating databases in MongoDB.
This guide will show two ways to create a collection in MongoDB.
Prerequisites
- MongoDB installed (follow our guide to install MongoDB on Ubuntu).
- Access to the command line/terminal.
Method 1: Create a Collection in MongoDB via createCollection()
The first way to create a collection is to use the built-in createCollection()
database method. There are four different collection types in MongoDB:
- Standard. The most common collection type. Used for general data storage.
- Capped. Fixed-size collection that overwrites the oldest documents when reaching the size limit.
- Clustered. Implements a clustering index to improve query performance by indexing documents.
- View. A read-only collection and an aggregate of one or more base collections.
Each collection type has options specific to that type. The sections below show the exact syntax and additional options.
Create Collection Syntax
To create a collection using this method, use the following syntax:
db.createCollection("[collection_name]", [options])
The statement has two parameters:
[collection_name]
. The name of the collection. The name must be unique within a single database.[options]
. Optional parameter to specify collection type and additional settings.
For example, to create a collection called myCollection
, run:
db.createCollection("myCollection")
The MongoDB shell confirms the creation was successful. Verify the collection is available with:
show collections;
The newly created collection appears on the list.
Create Collection Options
There are several options that help specify additional settings when creating a collection using the createCollection()
method. Refer to the table below for more details on some of the available options:
Option | Description |
---|---|
capped | Indicates if the collection is capped (set to true to create a capped collection). |
max | (Optional) Specifies the maximum number of documents in a capped collection. |
size | Sets the maximum size for a capped collection in bytes. When this size limit is reached, the oldest documents are overwritten. |
clusteredIndex | Defines the clustering index for document organization based on key , improving query performance. |
key | Sets the key pattern for the clustering index. |
unique | (Optional) Ensures values are unique in a clustered collection. |
viewOn | Determines the base collection on which a view is based. |
pipeline | Defines an array of aggregation steps to transform documents from base into view. Each step performs a transformation. |
storageEngine | Specifies the storage engine and determines how data is stored and managed. |
For example, to create a capped collection, see the following example:
db.createCollection("myCappedCollection", {
capped: true,
size: 10000,
max: 100
});
The query creates a capped collection (capped: true
) called myCappedCollection
. It also adds additional options (size
and max
) and constraints specific to that collection type.
Method 2: Create a Collection in MongoDB During the insert Process
Another way to create a collection in MongoDB is during the insert process. If a collection does not exist, the process automatically creates one and inserts the provided data.
For example, to create a collection and insert a document, use the following command:
db.myCollection.insertOne({
id: 1,
name: "example"
});
If myCollection
does not exist, the command creates it during the insert process. Compared to the previous method, the insert process quickly creates a collection and adds data in a single step.
Conclusion
This guide showed two methods for creating a collection in MongoDB. A collection organizes documents in a MongoDB database, similar to how a table works in a relational database.
Next, see how to create and deploy a sharded MongoDB cluster in our MongoDB sharding guide.