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:
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.
Parameter | Description |
Name | Name of the collection to be created |
Options | (Optional) Specify options about memory size and indexing |
Below is an example of CreateCollection() mongodb query with options:
db.createCollection("turorialsJar", { capped : true, size : 1200000, max : 2000 } );
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:
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:
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.
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.
- MongoDB Operators Tutorial – What are Different Operators Available? - October 5, 2019
- MongoDB Projection Tutorial : Return Specific Fields From Query - March 9, 2019
- MongoDB Index Tutorial – Create Index & MongoDB Index Types - July 6, 2018
nice work