BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Create Database in MongoDB

Last Updated: September 16, 2017 by Chaitanya Singh | Filed Under: MongoDB Tutorial

In this tutorial, we will see how to create and use a database in MongoDB.

MongoDB Create Database

Start the MongoDB service by typing this command:

net start MongoDB

You should see a message like this:

The MongoDB service is starting..
The MongoDB service was started successfully.

Navigate to the bin directory of MongoDB as shown in the screenshot below:
Starting MongoDB Service

Now we should be working in the MongoDB shell. To run the MongoDB shell, type the following command:

mongo

Once you are in the MongoDB shell, create the database in MongoDB by typing this command:

use database_name

For example I am creating a database “beginnersbook” so the command should be:

use beginnersbook

Note: If the database name you mentioned is already present then this command will connect you to the database. However if the database doesn’t exist then this will create the database with the given name and connect you to it.
Creating Database in MongoDB

At any point if you want to check the currently connected database just type the command db. This command will show the database name to which you are currently connected. This is really helpful command when you are working with several databases so that before creating a collection or inserting a document in database, you may want to ensure that you are in the right database.

> db
beginnersbook

Currently Connected Database using DB command

To list down all the databases, use the command show dbs. This command lists down all the databases and their size on the disk.

> show dbs
admin  0.000GB
local  0.000GB

show dbs command list all databases

As you can see that the database “beginnersbook” that we have created is not present in the list of all the databases. This is because a database is not created until you save a document in it

Now we are creating a collection user and inserting a document in it.

We will learn how to create collection and document in the next tutorials.

> db.user.insert({name: "Chaitanya", age: 30})
WriteResult({ "nInserted" : 1 })
> show dbs
admin          0.000GB
beginnersbook  0.000GB
local          0.000GB

Database is created
You can now see that the database “beginnersbook” is created.

❮ PreviousNext ❯

Top Related Articles:

  1. MongoDB Tutorial for Beginners
  2. Introduction to MongoDB
  3. Drop Database in MongoDB
  4. Introduction to NoSQL Databases
  5. MongoDB Query Document using find() method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

MongoDB Tutorial

  • MongoDB Tutorial
  • NoSQL Introduction
  • Introduction to MongoDB
  • Mapping Relational Database to MongoDB
  • Install MongoDB
  • Create Database
  • Drop Database
  • Create Collection
  • Drop Collection
  • Insert Document
  • Query Document
  • Update Document
  • Delete Document
  • MongoDB Projection
  • MongoDB limit() and skip()
  • MongoDB sorting
  • MongoDB Indexing

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap