MongoDB Create Database Tutorial

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

A Complete Guide to Create and Insert Database in MongoDB:

Our previous tutorial in this Free MongoDB training series explained to us all about the Installation of MongoDB on Windows.

In this tutorial, we will see how to Create & Insert Database in MongoDB.

The most basic step in MongoDB is to make a Mongo Database and a Mongo Collection in it. Remember, that Database bears all collections while collections store documents in them.

MongoDB Create Database Tutorial

Furthermore, mongo documents would be having specific Field Names & Field Values as well.

Figure 1: Field Names & Field Values in MongoDB.

Field Names and Field Values in MongoDB

The Field Names are “staff_id” & “Staff_member_name” while the Field Values are “01” & “Jhon“. Just like this, multiple mongo documents together make up a mongo collection.

MongoDB – Create Database

Creation of a database is so easy. All you have to do is to put “use” before the name of the database and press Enter.

Figure 2: Mongo list collections with the command “Show dbs”.

Create Database

Description: In MongoDB, the command “use” is used to create a database. If there is no database in the MongoDB already, then one will be made.

Upon successful execution, you will get the result as shown below.

Figure 3

successful execution of new db

Switching into the database is itself a successful execution of the “use” Command.

Recommended reading =>> How to create a database in MySQL

Create Collection Using insert ()

As we have created a database. Now, let’s create a mongo collection in it. For this purpose, a user must insert certain records which make up a mongo document in collection i.e. via Field Names & Field Values.

If there is no collection in the database, then one would be made. The command given below would be used to generate a collection.

Figure 4

Create Collection Using insert ()

The following output would come upon successful execution.

Figure 5: In Shell

MongoDB results - In Shell

Figure 6: In Robo 3T

Execution Result - In Robo 3T

The following output would come upon successful execution.

Figure 7

Output in Robo 3T

Description: As you see, using the insert command, we have successfully generated a collection in the database.

How Mongo Documents Have Been Made?

As we know, MongoDB has insert () command to make a collection by inserting documents into it.

Let’s see how it is done!!

Step 1: Launch MongoDB by writing commands “mongod” and “mongo” on command prompt. You can check the previous tutorial on Installation of MongoDB on Windows for detailed information on this.

Step 2: Write insert () command, which is in our case is:

db.staff.insert
(
             { -+
                       "staff_id": 01,
                       "staff_member_name": "Jhon"
              }
)

Press Enter.

Description:

  • The first component of the command is Insertion of the statement, through which a user can insert documents into the collection.
  • The second part of the command is adding Field Names & Field Values, which is the actual stuff in documents.

If the execution is done successfully, then the given output would be displayed.

Figure 8

insertion done successfully

The output given above displays that the insertion has been done successfully.

Conclusion

In this tutorial, we learned how to create a database and insert mongo collections and documents in it. We also had a look at the step by step guide for database creation and insertion of mongo collection.

Also read =>> How to use MongoDB with Flask

Stay tuned to check out our upcoming tutorial on Using arrays for an insert statement on MongoDB!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment