How MongoDB Create Database – Learn in 4 Steps

FREE Online Courses: Knowledge Awaits – Click for Free Access!

We have learned MongoDB Data Modeling in the previous chapter. After learning that, we have to start using MongoDB. For that, we must know how MongoDB Create Database. In this article, we will see how a database is created in MongoDB.

So, let’s start MongoDB create database tutorial.

MongoDB Create Database

If you are looking for a command to create a database, you must stop now. This is because MongoDB does not provide any command to create a database. In fact, we don’t create a database in MongoDB.

Unlike SQL, where we need to create a database, tables and then insert the values manually, MongoDB creates the database automatically. You just need to save a value in the defined collection with the preferred name.

You don’t even have to mention that you want to create a database. However, you can create collections manually. We will discuss that later.

a. The “use” Command

If there is no database, use the following command to define the name for your database and the database is created if it doesn’t exist already.
Syntax

use database_name

For example, Let’s say the database name is “dataflair”.

>use dataflair
Switched to db dataflair

b. Check Selected Database

You can check that database you have selected already. Follow this command to do that.

>db
dataflair

c. List Database

If you don’t know the databases that already exist in the system, you can list them and know if they do. Just use the following command for this.

>show dbs
local     0.52938GB
test      0.49231GB

If your database is not in the list, your database has not been created yet. To create a database you must save at least one document in it.

d. Save a Document

Run the following command to insert a document into the database.

> db.user.insert({name: "Ankit", age: 23})
WriteResult({ "nInserted" : 1 }) 
> show dbs
local          0.52938GB
dataflair      0.49231GB
test           0.49231GB

Now your database with the name “dataflair” has been created. 

This was all about MongoDB Create Database Tutorial. Hope you like our explanation.

Conclusion

Hence, in this MongoDB Tutorial, we saw that MongoDB has its own feature to create the database. We need not do so much effort to create a database.

Now we have learned how MongoDB create a database. In the next article, we will see MongoDB Drop Database. Furthermore, if you have any query, feel free to ask in a comment section.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

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