MongoDB Create A Collection Tutorial with Example

Last updated on: by Digamber
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:

ArgumentDetails
collection_nameThe 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.

OptionsTypeDetails
cappedbooleanIt’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.
autoIndexIdbooleanIf 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.
sizenumberThis 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.
maxnumberIt’s an optional value, the max field in mongoDB defines the maximum number of documents passed in a capped collection.
validatordocumentThe 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
contacts

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

db.collection.insert()
db.users.insert(
   {
     "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
contacts
users

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.