MongoDB - Create a Collection

You can create a collection using the createCollection() method, or on the fly as you insert a document.

Collections are like containers for related documents. They are typically used to group documents of a similar topic. For example, you could have collection names such as users, pageviews, posts, comments, etc.

When we created our database, we created a collection called artists. This collection will contain documents with artist details, such as the artists' names, albums they've released, etc.

Two ways to Create a Collection

Here are two ways of creating collections:

On the Fly

When you use the insert() method to insert a document, you specify the collection that the document will be inserted into. If the collection doesn't already exist, it will be created.

This is the method that we used previously when we created our artists collection while inserting a document.

Here's the code that we used:

In this case, the artists collection didn't previously exist so it was created for us.

Using the createCollection() Method

You can also create collections using the createCollection() method. This allows you to create a collection without inserting a document.

Here's an example of using the createCollection() method:

With Options

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

Here's an example:

The fields available as of MongoDB version 3.2 are as follows.

FieldTypeDescription
capped boolean 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 boolean 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 number Maximum size in bytes for a capped collection. Only used with capped collections (it is ignored in other collections).
max number 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.
usePowerOf2Sizes boolean Only available in the MMAPv1 storage engine. This field has been deprecated since version 3.0.
noPadding boolean Only available in the MMAPv1 storage engine. Disables the power of 2 sizes allocation for the collection. Defaults to false.
storageEngine document Only available 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 document 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 string Allows you to specify how strictly any validation rules are applied to existing documents during an update. Possible values:
offNo validation occurs for inserts or updates.
strictValidation occurs for inserts and updates. this is the default value.
moderateValidation rules are only applied to existing valid documents, but not to existing invalid documents.
validationAction string 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.
errorSpecifies that an error occurs when an invalid document is being written, and the operation fails.
warningSpecifies that invalid documents can still be written, and that the warning will be logged.
indexOptionDefaults document 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> }