How MongoDB Create Database – Learn in 4 Steps


1. Objective

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.

How MongoDB Create Database - Learn in 4 Steps

How MongoDB Create Database – Learn in 4 Steps

2. 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 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.

Let’s Explore the MongoDB Tutorial For Beginners in Detail

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.

Read about Features of MongoDB

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.

3. 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 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.

Related Topic- Advantages & Disadvantages of MongoDB

For reference

Leave a comment

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