MongoDB Create Collection Command With Example

In the last article, you have learned about creating and dropping database in MongoDB. In this, we will discuss MongoDB create collection command with the example. Before moving forward, let’s see what exactly collections are. In simple terms, collections are like a container for related documents. In SQL databases, we have TABLES. Similarly, in MongoDB we have COLLECTIONS. These are used to group documents of similar topics. For example, for this blog, we may have various collections like – categories, authors, blog posts, comments, tags etc.

MongoDB Create Collection Command

We can create collections in MongoDB from two different ways:

Create Collection Explicitly

Create Collection On The Fly

Let’s discuss more on these create collection commands now:

MongoDB Create Collection Explicitly

To create collection explicitly, the developer needs to use the createCollection() method. This allows you to create a collection without inserting a document. Many times we might need NOT to insert the document and just need to add some collections in the starting and later on add some documents. In those cases, we will go for createCollection() method.

Below is create collection command using the createCollection() method:

CSS

1

db.createCollection("turorialsJar");

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

In the command, name is the name of collection to be created. Options is a document and is used to specify configuration of collection.

It shows that it is a capped collection. Following are the different options that you can specify for the collections:

Field

Description

capped

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

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

Maximum size in bytes for a capped collection. Only used with capped collections (it is ignored in other collections).

max

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.

noPadding

Only available in the MMAPv1 storage engine. Disables the power of 2 sizes allocation for the collection. Defaults to false.

storageEngine

Available only 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

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

Allows you to specify how strictly any validation rules are applied to existing documents during an update.

validationAction

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.

indexOptionDefaults

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

2) MongoDB Create Collection On The Fly

MongoDB creates collection automatically when you insert any document using the insert()method. The reason is while using insert() document, we specify the collection in which the document will be inserted to. If the collection exists, it will add the document to existing collection. But if the collection does not exist, then it will create a collection.

Below is the create collection command using insert() method:

CSS

1

db.tutorialsJar.insert({tutorialType:"MongoDB"});

Here, I have mentioned tutorialsJar collection where I want to insert the document to. So, it will create tutorialsJar collection (if it is not already present).

If you want to see the inserted document, use the find() command.

Syntax:

CSS

1

db.tutorialsJar.find()

Below is the snapshot of the above example:

Once you have created collections, you can see all the collections using show collections command.

1

show collections

It will list out all the collections available in the database you are into.

That’s all in this article. Do let us know if you have any queries in the comment section below. Also, do not forget to share it with your friends.

Related

One thought on “MongoDB Create Collection Command With Example”

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

All the data shown above will be stored by TutorialsJar on https://www.tutorialsjar.com/. At any point of time, you can contact us and select the data you wish to anonymize or delete so it cannot be linked to your email address any longer. When your data is anonymized or deleted, you will receive an email confirmation. We also use cookies and/or similar technologies to analyze customer behaviour, administer the website, track users' movements, and to collect information about users. This is done in order to personalize and enhance your experience with us.