How to Create MongoDB Collections

Read it in 5 Mins

Last updated on
15th Mar, 2021
Published
15th Jul, 2020
Views
8,051
How to Create MongoDB Collections

In MongoDB, data is stored in the form of documents. These documents are stored inside collections and collections are placed inside database. A database in MongoDB can have several collections. 

Collections in MongoDB can be created implicitly while we are importing the data or explicitly by specifying the ‘createCollection’ command. Usually the implicit creation is the most common method but for instances where we need to create a capped collection or collection with schema validation rules explicit method is used. 

In this article, we will explore both the implicit and explicit methods to create acollection.  

Implicit Creation 

In this method, we do not specify the create collection command. The collection is created automatically when data is imported from the application. This is the most common method used for creating collection in MongoDB. We can insert the data from the mongo shell or directly from the application. 

Example 1: Create a collection named implicit through the mongo shell.

In the above

how to Create a collection named implicit through the mongo shell
 example we have inserted one row inside the implicit collection which was not present before the document insertion. As the row is inserted we can see a new collection is created. This collection can now be used for future data insertion and is persistent. 

Example 2: Create a collection named application through python application. 

Steps:  

First,We need to establish connection to themongodb server. 
conn = MongoClient(‘host’, port-number) 
Here, host and port will be of the server hosting mongodb. 
Switch to the database where we want to import data. 
db = conn.databasename 
Create a collection object 
Collection = db.application 
Insert data 
Collection.insert_one({“name” : “implicit”}) 

This will create a collection named application and insert data inside it.

how to Create a collection named application through python application

Now we need to verify if the data inserted is present inside the collection.  

how to Create a collection named application through python application

We can see that the application collection is created inside the implicitdb database and the rows we inserted are present.

Explicit Creation 

The explicit method to create collection is mainly used in case there is need to create special collections like capped collections or collections with document validation rules.  

Syntax

db.createCollections(name,options) 

Below is the description of the arguments passed in the createCollection() method.

ParameterTypeDescription
nameStringName that we want to give to collection.
optionsdocumentThis is optional, depending on the type of collection we want to create we can specify different options.

The ‘name’ parameter is of type string while the ‘option’ parameter is document. Let’s explore the ‘option’ parameter document to see some of major options while creating collection explicitly.

FieldDescription
cappedSpecify value ‘true’ if want to create capped collection
sizeMaximum size in bytes for capped collection
maxMaximum number of documents allowed in capped collection.
StorageEngineTo specify storage engine options. Valid only for WiredTiger
writeConcernThe write concern that will be used for the collection for write operations.
ValidatorAllows to specify validation rules for collection

Example: Creating a collection named ‘explicit’ explicitly

Creating a collection named ‘explicit’ explicitly

We can see ‘explicit’ collection is created using the create eCollection command. 

Capped Collection 

Capped collections are fixed size collections that start to overwrite the oldest log entries when the size reaches the maximum limit. The oplog that is used for MongoDB replication are capped collections.  

Example: Create a collection named ‘cc’ which is capped collection.

Create a collection named ‘cc’ which is capped collection

In above example we have create capped collection ‘cc’ explicitly using the createCollection command. While creating the capped collection we need to specify the collection size and maximum number of documents it can have. 

Creating Collection with Document validation 

Documents with validation in MongoDB compare each insert and updates against the validation rules set for the collection. These rules are specified using validator option in the createCollection command. The row is inserted or updated if the validation is passed and rejected if the validation fails. 

Example: create a collection named ‘validate’ with validation rules 

We will create a collection named ‘validate’ with validation rules and try to insert data that does not meet the validation requirement.

create a collection named ‘validate’ with validation rules

We can see document insertion failed as it did not pass the validation of schema defined by the validator. 

Access Needed to Create Collections 

To read and write data inside MongoDB we should have proper authentication/authorization. Authentication defines who can access the system and Authorization controls what activity can be performed by whom. MongoDB uses RBAC [Role Based Access Control] where access is provided to set of roles and then these roles are assigned to users.  

Below is the access matrix for createCollection.

Access matrix for createCollection

User with built-in role readWrite can also create collection inside the database. 

Locking Behaviour of createCollection() command 

From MongoDB version 4.2 onwards db.createCollection() obtains an exclusive lock on the specified collection or view for the period of the operation. This lock is being held typically for a very short time but during this operation all other operations must wait for the lock to be released. If we are creating view additional lock will be placed on collection system.views and will block all other view creation until this command completes. 

In summary

In this blog, we have seen how collection can be created in MongoDB both implicitly and explicitly. The implicit way to create collection inside the MongoDB database is the most common method, widely used by developers.  

The explicit method is needed when special kinds of collections need to be created such as capped collection or if we want to place validation rules for our schema. We have also pored over the access matrix which helps developers to be provided with adequate access so that they can create collections implicitly. Finally, we saw the behaviour of locks when the collection is created. 

To advance your career in the much sought after MongoDB, explore the MongoDB developer course. You will learn critical aspects of MongoDB and use it to perform real-time analytics, data management, and other activities with a unique blended learning experience.  

In the MongoDB administrator course, you will learn about architecture and installation, CRUD operations, schema design, data modelling and how replication works. You will also learn to use various GUI tools required to perform DBA tasks.


Find easy solutions to Database management. Explore our MongoDB online workshops here.

Profile

KnowledgeHut

Author
KnowledgeHut is an outcome-focused global ed-tech company. We help organizations and professionals unlock excellence through skills development. We offer training solutions under the people and process, data science, full-stack development, cybersecurity, future technologies and digital transformation verticals.