How To Create a Collection in MongoDB Using Implicit And Explicit Methods

In this tutorial we will learn how to create a collection in MongoDB using implicit and explicit methods with examples even if you have basic-to-no knowledge. Before all of that we need to understand that like in the previous post we learned how to create a MongoDB database. So that database is basically an empty container unless we add some data in the form of collections and more.

What is a Collection in MongoDB?

A collection is analogous to a table in relational databases. A MongoDB collection is analogous to a relational database table where we use CREATE TABLE for creating the tables for the database. Since MongoDB is not a relational database so it stores data in the form of documents again each of them get stored automatically in a collection so we need to create collections before adding any documents to those collections.

How to Create A MongoDB Collection?

This tutorials shows two different options to create MongoDB collections: Implicit and Explicit. Check the example for each one of them.

How to Create a Collection in MongoDB Using Implicit Method

You can create a collection implicitly by simply adding a document to a non-existent collection. When you do this, the collection is created if it doesn’t already exist.

Here’s an example of implicitly creating a collection:

db.pets.insert({ name: "Fetch" })

This will create the collection Pets for us with adding a document as well. So if there’s no such previously created collection with the name Pets it will create the new collection with that name, if it already exists it will add the document in the existing collection.

Basically, the syntax goes like this:

db.<collection>.insert()

How to Create a Collection in MongoDB Using Explicit Method

The next we are going to create a MongoDB collection explicitly with the db.createCollection() syntax. As compared to the previous method, this one is very detailed with certain specifics that lets us set the size and rules for documents. It’s more like SQL’s CREATE TABLE function.

We simply use db.createCollection()method to create a collection such as:

db.createCollection("employees")
Result
{ "ok" : 1 }

In this example, we didn’t specify any thing in particular so technically it’s better to create a collection explicitly. We can specify details only through explicit method.

Here’s an example of specifying some options when creating a collection:

db.createCollection(
    "products", 
    { 
        capped : true, 
        size : 7500500, 
        max : 7000 
    } 
)

Result

{ "ok" : 1 }

Explanation

We have used capped argument which has allowed us to specify whether or not the collection’s size should be capped (which is limited to a certain size). We need to specify the max size in the respective field to specify true which uses bytes for capped collections. If the max size is reached our MongoDB database will remove the older documents to add the new ones. Apart from the capped collections, it’s ignored. If a capped collection reaches the maximum size before it reaches the maximum number of documents then the older documents will be replaced. So make sure to understand how many documents we can add on a certain size before initialing the method. We can use 3 different methods to specify db.createCollection().

The Syntax

See below how the Syntax has to be like:

db.createCollection( <name>,
   {
     capped: <boolean>,
     autoIndexId: <boolean>,
     size: <number>,
     max: <number>,
     storageEngine: <document>,
     validator: <document>,
     validationLevel: <string>,
     validationAction: <string>,
     indexOptionDefaults: <document>,
     viewOn: <string>,
     pipeline: <pipeline>,
     collation: <document>,
     writeConcern: <document>
   }
)

See db.createCollection() from the official MongoDB documentation for a detailed explanation of each option we can also create a Clustered collection instead of Capped. In this tutorial we have focused on using Capped collection method to create a collection in MongoDB using Explicit option. Let us know in the comments for further queries related to creating collections in MongoDB databases.