Menu Close

How to Create Database and Collection in MongoDB

How to Create Database and Collection in MongoDB

In this MongoDB tutorial, You will learn how to create database and collection in the MongoDB server using the MongoDB compass application and MongoDB shell with the help of the examples.

MongoDB does not provide any ‘create‘ command to create databases like traditional SQL databases like MySQL, SQL Server, and PostgreSQL.

Note:- Databases in MongoDB indicate the databases in SQL Like databases engines and collections in MongoDB indicate the tables in the  SQL databases engines like MySQL, SQL Server, and PostgreSQL.

Let’s see the process of creating a database and collection in MongoDB using a compass and Mongodb shell.

Make sure You have installed the MongoDB NoSQL server on your machine.

Create Database and Collection using MongoDB Compass

MongoDB Compass is a GUI ( Graphical User Interface ) application to manage MongoDB databases like creating databases, creating collections, creating documents, deleting databases, delete collections, and documents.

MongoDB compass is a very easy-to-use application, you don’t need more technical knowledge to use this application, You need to follow some basic steps and things will already happen.

Let’s start.

  • Open MongoDB Compass Application. Click on the Connect button to connect to the MongoDB server.
Create Database and Collection using MongoDB Compass
  • Now, You need to click on the Plus Icon in the left side panel to create a Database and collection.
Create Database and Collection using MongoDB Compass
  • Provide database name and collection name, In my case database name is ProgrammingFunda, and the collection name is an employee and after that click on Create Database.
Create Database and Collection using MongoDB Compass
  • Now, the Created database will appear on the left side as you can see below and collections will appear inside the database.
Create Database and Collection using MongoDB Compass

This is how you can create a database and collection in MongoDB using MongoDB compass. Now, Let’s see how we can create using MongoDB shell.

Create Database and Collection using MongoDB Shell

MongoDB Shell:- MongoDB shell is a command line interface to manage database-related tasks and you can perform administrative tasks. You can perform almost all the operations that you can perform from the MongoDB Compass application.

Let’s create a database and collection using MongoDB shell.

  • Open the command prompt in your machine and type ‘mongosh‘ to enter into the mongo shell.
Create Database and Collection using MongoDB Shell
  • After entering the MongoDB shell, There is no ‘create‘ command to create a database. You need to use the ‘USE DATABASE_NAME’ command to create the database inside the MongoDB. Replace DATABASE_NAME with your database name. In my case, the database name is ‘ProgrammingFunda‘. After using the ‘USE DATABASE_NAME‘ command, you will switch to provide the database name.
Create Database and Collection using MongoDB Shell
  • Now, let’s display all the databases using the show dbs command. As you can see below. The ‘ProgrammingFunda‘ database is not showing because MongoDB only creates the database when you store content inside that database. The data could be a collection or a document.
Create Database and Collection using MongoDB Shell
  • To create a collection in the database, you can use the createCollection() function and pass the collection name inside that function. In my case, I am creating an employee collection name as you can see below.
Create Database and Collection using MongoDB Shell
  • Now, let’s display the database name again by using the ‘show dbs‘ command, and this time ‘ProgrammingFunda‘ database is showing as you can see.
Create Database and Collection using MongoDB Shell

So this is how you can create database and collection in MongoDB using MongoDB shell.


FAQs

What is MongoDB?

Ans:- MongoDB is one of the most popular open-source NOSQL databases that store data in JSON-like documents.

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. It’s similar to a table in a relational database management system but with a dynamic schema.

How is MongoDB different from traditional SQL databases?

Ans:- As we know, MongoDB is a NoSQL Database, which means it does not require a row and column table structure that is found in traditional databases like SQL. It stores data in a like format.


Conclusion

In Today’s article, we have seen how to create database and collection in MongoDB by using MongoDB compass and MongoDB shell.
You can use any tool at your convenience.

If you found this article helpful, please share and keep visiting for further MongoDB tutorials.

Thanks for your timeā€¦.

Online MongoDB Tutorial 2024
How to Create a Collection in MongoDB

Related Posts