Create a Collection in MongoDB

When you create a database in MongoDB, your database is basically an empty container for which you can add one or more collections to.

A collection is analogous to a table in relational databases.

In relational databases, you can use CREATE TABLE to create each table that you want in the database.

But MongoDB is not a relational database, and it stores its data as documents. Each document is stored in a collection.

This article shows you how to create a collection in MongoDB.

Two Options

You have two options for creating collections in MongoDB:

  • Implicit
  • Explicit

Here’s an example of each one.

Create a Collection – Implicitly

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" })

That creates a collection called pets, and inserts a document into it.

Actually, it only creates the collection if it doesn’t already exist. If it already exists, then it simply inserts the document into the existing collection of that name.

Basically, the syntax goes like this:

db.<collection>.insert()

Where <collection> is the name of the collection.

Create a Collection – Explicitly

You can also create collections explicitly with the db.createCollection() method. This method allows you to specify various options, such as setting the maximum size or the documentation validation rules.

This is kind of analogous to the CREATE TABLE statement in SQL. Having said that, MongoDB doesn’t require that you specify columns, data types, etc like you’d need to specify with the CREATE TABLE statement when using a relational database.

Here’s an example of using the db.createCollection() method to create a collection:

db.createCollection("employees")

Result:

{ "ok" : 1 } 

In this example, I didn’t specify any options. Therefore, there was no real benefit of creating it like this verses creating it implicitly (like in the previous example).

However, if you wanted to specify options for your collection, then creating it explicitly is the way to go.

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

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

Result:

{ "ok" : 1 } 

Here’s an explanation of the options I provided in this example:

  • The capped argument allows you to specify whether or not the collection’s size should be capped (i.e. not allowed to grow beyond a certain size). If you specify true, you must also set a maximum size in the size field.
  • The size argument sets a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The size field is required for capped collections and ignored for other collections.
  • The max argument allows you to specify the maximum number of documents allowed in the capped collection. If a capped collection reaches the size limit before it reaches the maximum number of documents, MongoDB removes old documents. Therefore, be sure that the size argument is sufficient to contain the number of documents specified with the max argument.

These are three of the various options that you can specify with the db.createCollection() method.

Syntax and More Details

The full syntax (as of this writing) looks like this:

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.