Menu Close

How to Create a Collection in MongoDB

How to Create a Collection in MongoDB

In this MongoDB tutorial, we will see everything about how to create a collection in MongoDB with the help of the MongoDB compass application and Mongosh command line interface with the help of the example.

In MongoDB, the collection is the same as Tables in RDBMS ( Relational Database Management System ), There are two multiple ways to create a collection in MongoDB.Let’s see all of them using examples.

What is Collection in MongoDB?

Collection in MongoDB is a table to store a collection of documents, Here collection represents the table in SQL Database and documents represent the records of tables in SQL Databases.

MongoDB provides two ways to create a collection, using the MongoDB compass GUI application.

Create a collection in MongoDB using MongoDB Compass

MongoDB Compass is a GUI ( Graphical User Interface ) application that is used to perform almost all kinds of operations like creating a database, creating a collection, deleting a collection, deleting a database, creating documents, updating documents, etc.

Let’s see how we can create a collection in MongoDB.

Create Collection in Existing Database

If you want to create a MongoDB collection in the existing database with the help of the MongoDB compass application, Then follow these steps.

  • Open the MongoDB Compass application.
  • Hover the mouse cursor on the Database name and Click on the plus icon.
How to Create a Collection in MongoDB

  • Provide the collection name and click on Create Collection.
How to Create a Collection in MongoDB

  • Congratulations, Your collection has been created. The collection will be shown in the database section as you can see below.
How to Create a Collection in MongoDB

Create Collection in New Database

To create a collection new database, You have to follow these basic steps.

  • Open the MongoDB Compass application.
  • Click on the Plus icon on the database.
Create Collection in New Database

  • Provide the database name and collection name and click on Create Database.
Create Collection in New Database

  • Congratulations, the New database and the new collection are successfully created as you can see below.
Create Collection in New Database

This is how you can create a collection in MongoDB with the help of the MongoDB compass application.

Let’s create a mongodb collection with the help of the mongosh.

What is Mongosh?

Mongosh is a command line tool to perform database operations as well as administrative tasks.

Create a collection in MongoDB using Mongosh

There are two ways to create a collection in MongoDB using the Mongosh command line interface. Let’s explore both methods one by one.

Method 1

  • Open the command prompt on your computer.
  • Type mongosh to enter in the MongoDB shell.
Create a collection in MongoDB using Mongosh
  • Type the show dbs command to display all existing databases, In my case, three databases exist.
Create a collection in MongoDB using Mongosh
  • To create a new collection in an existing database, you will need to switch inside that database. Use the ‘use database_name’ command to switch into the database, Here I am switching to the ‘ProgrammingFunda‘ database.
Create a collection in MongoDB using Mongosh
  • Now, you can use the createCollection() database method to create a new collection. This function takes the collection name as a parameter. Here I am creating a new collection called country.
Create a collection in MongoDB using Mongosh
  • After executing the createCollection() method you will get the ok message, That means your new collection is created successfully.
  • You can display all the collections using the ‘show collections‘ command.
Create a collection in MongoDB using Mongosh

Note:- If you are using the 'use database_name' command and your database name does not exist then the use command will create the the new database.

Method 2

In all the above collections, we did not store any documents. MongoDB provides a collection method called insert(). The insert() method is used to insert the single or multiple documents inside the collection.

Note:- The insert() collection method also creates the collection if the collection does not exist.

Let’s create a collection using the insert method.

For example, I am going to create a collection called student inside the ProgrammingFunda database that will store information about the students like first_name, last_name, and age.

Go inside the MongoDB shell switch into the ProgrammingFunda database just like Method 1 and use the below command.

db.student.insert({"first_name": "Programming", "last_name": "Funda", "age": 4})

In the above command, the student is the name of the collection which was not showing till now but after executing the command you will get a message like below. This means student collection is created along with a document {"first_name": "Programming", "last_name": "Funda", "age": 4}.

{
  acknowledged: true,
  insertedIds: { '0': ObjectId('6623b55a048994138816c9b5') }
}
Note:- You can compare document in collection with record into SQL database table.

You can also use the show dbs command to display your created collection.

This is how we can use the insert method to create the document.


FAQs

What is MongoDB?

Ans:- MongoDB is one of the most popular open source NOSQL database systems that stores data in JSON-like format called document.

What is a Database in MongoDB?

Ans:- In MongoDB, a database is a container for collections. It holds a set of collections, similar to how a traditional relational database would hold tables.

What is a collection in MongoDB?

Ans:- A collection in MongoDB is a grouping of MongoDB documents or records. It’s similar to a table in a Relational Database Management System (RDBMS) but with a dynamic schema.

How does a collection duffer from a table?

Ans:- MongoDB stores its data in the collections instead of tables. A collection holds one or more BSON documents. BSON is a binary representation of JSON documents. Documents are similar to records in relational database tables. Each document has one or more fields.


Conclusion

In this MongoDB tutorial, we have seen all about how to create a collection in MongoDB using the MongoDB compass application and Mongosh command line interface command with the help of the example.

Collection in MongoDB is nothing but it is a table to store collections. If you found this article helpful, please share and keep visiting for further Mongodb tutorials.

Thanks for your valuable timeā€¦..

How to Create Database and Collection in MongoDB
How to Insert Documents in MongoDB Collection

Related Posts