MongoDB Create A Collection Tutorial with Example

By Digamber Rawat Last updated on
In this mongoDB tutorial, we are going to learn how to create a collection in MongoDB database using MongoDB shell.

In MongoDB, the first step is to create a collection so that we can store all the data into the collections. Collection refers to a `key`: `value` pair JSON data file which holds the user information like name, email, age, etc.

There are 2 methods offered by mongoDB to create collection. Let’s understand those 2 methods through which we can create a collection in mongoDB:

Understand MongoDB createCollection() Method

Creating a collection in mongoDB is very simple using the given below method. This method helps you create a data collection without inserting a document in mongoDB.

The Syntax

db.createCollection(collection_name, options)

Understand createcollection() method a bit more:

Argument Details
collection_name The first argument is the name of the collection in the database.
options It’s an optional parameter however you can pass the collection options like: capped, autoIndexId, size etc.

In the next, step we’ll check some of the options for managing the collection in mongoDB.

Options Type Details
capped boolean It’s an optional value when you set it to `true` it creates a capped collection. The capped collection is also known as the fixed size data collection, and it overwrites the oldest entries when it reached the max size limit. If you set it to true, you should also set a maximum size in the size field.
autoIndexId boolean If you set it to false, then you can disable the auto-creation of an index on the _id field. Note: it was deprecated since version 3.2.
size number This value should only be used with the capped collection, and the size field is referred to the maximum size in bytes for a capped collection.
max number It’s an optional value, the max field in mongoDB defines the maximum number of documents passed in a capped collection.
validator document The validator field let us define the validation rule for the data collection. Validation can only be applied when updating and inserting the data in mongoDB.

For more details you can visit MongoDB official documentation here.

Check Databases in MongoDB

Run command to check the total number of database in your local system, although we are using `mongodatabase`.

show databases

# mongodatabase
# database2
# database3
# database4

Using MongoDB Database

Run below command to select mongoDB database using MongoDB shell:

use mongodatabase
use mongodatabase

# switched to db mongodatabase

Create a Collection in MongoDB

We are creating a collection using createCollection() mongoDB method without the options:

> db.createCollection("contacts")
{ "ok" : 1 }

Check MondoDB Collection

Run command to check the existing collection in the mongoDB.

$ show collections

Creating a Collection using MongoDB insert() Method

In this method, let’s find out how to easily create a collection by inserting a record in the collection. Remember, if the collection doesn’t exist, then the new collection will be created.

The Syntax

     "userid" : 1,
     "name" : "John"

After executing the command, following output will be displayed in your terminal:

WriteResult({ "nInserted" : 1 })

Execute the command to check out the newly created collection which wasn’t available earlier.

> show collections

Digamber Rawat
Digamber Rawat

Full stack developer with a passion for UI/UX design. I create beautiful and useful digital products to solve people’s problem and make their life easy.

Hire Me