MongoDB Create Collection Command With Example

In the last article, you have learned about creating and dropping database in MongoDB. In this, we will discuss MongoDB create collection command with the example. Before moving forward, let’s see what exactly collections are. In simple terms, collections are like a container for related documents. In SQL databases, we have TABLES. Similarly, in MongoDB we have COLLECTIONS. These are used to group documents of similar topics. For example, for this blog, we may have various collections like – categories, authors, blog posts, comments, tags etc.

MongoDB Create Collection Command

We can create collections in MongoDB from two different ways:

  1. Create Collection Explicitly
  2. Create Collection On The Fly

Let’s discuss more on these MongoDB create collection commands now:

  1. MongoDB Create Collection Explicitly


To create collection explicitly, the developer needs to use the createCollection() method. This allows you to create a collection without inserting a document. Many times we might need NOT to insert the document and just need to add some collections in the starting and later on add some documents. In those cases, we will go for createCollection() method.

Below is MongoDB create collection command using the createCollection() method:

You can also specify options for the collection by using the db.createCollection(name, options) syntax.

In the command, name is the name of collection to be created. Options is a document and is used to specify configuration of collection.

Parameter Description
Name Name of the collection to be created
Options (Optional) Specify options about memory size and indexing

Below is an example of CreateCollection() with options:

It shows that it is a capped collection. Following are the different options that you can specify for the collections:

Field

Description

capped

When set to true, creates a capped collection. A capped collection is a fixed-sized collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you must also set a maximum size in the size field.

autoIndexId

Specify false to disable the automatic creation of an index on the _id field. As of MongoDB version 3.2, this field is deprecated, and it will be removed in version 3.4.

size

Maximum size in bytes for a capped collection. Only used with capped collections (it is ignored in other collections).

max

Maximum number of documents allowed in the capped collection. Note that the size field takes precedence over the max field. If the collection reaches its size limit before the document limit has been reached, MongoDB will remove documents anyway.

noPadding

Only available in the MMAPv1 storage engine. Disables the power of 2 sizes allocation for the collection. Defaults to false.

storageEngine

Available only in the WiredTiger storage engine. Allows configuration to the storage engine on a per-collection basis when creating a collection. Syntax is as follows: { <storage-engine-name>: <options> }

validator

Allows you to specify validation rules or expressions for the collection. Note that validation is only applied when inserting and updating data. Therefore, data that already exists in the database is not validated (until it is updated).

validationLevel

Allows you to specify how strictly any validation rules are applied to existing documents during an update.

validationAction

Specifies whether an error should occur, or just a warning, when invalid documents are inserted. If an error, the invalid documents will still be inserted, but with a warning.
indexOptionDefaults Allows you to specify a default configuration for indexes when creating a collection. Accepts a storageEngine document with the following syntax: { <storage-engine-name>: <options> }

2) MongoDB Create Collection On The Fly


MongoDB create collection automatically when you insert any document using the insert()method. The reason is while using insert() document, we specify the collection in which the document will be inserted to. If the collection exists, it will add the document to existing collection. But if the collection does not exist, then it will create a collection.

Below is the MongoDB create collection command using insert() method:

Here, I have mentioned tutorialsJar collection where I want to insert the document to. So, it will create tutorialsJar collection (if it is not already present).

If you want to see the inserted document, use the find() command.

Syntax:

Below is the snapshot of the above example:

MongoDB Create Collection Command With Example

Mohit Arora
Follow me

Mohit Arora

Hi, I am Mohit Arora , a passionate blogger and addicted reader. Currently working as a Senior Systems Engineer in INFOSYS Ltd.
You can check my other blog - CatchUpdates.com

You can connect with me on Instagram Or Quora And Linkedin as well.
Mohit Arora
Follow me