create database in mongodb

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have seen how we can start mongoDB and connect to it in previous article, in this article we will see how to create a new database in mongodb and use it to insert or query from collections.

Create Database in MongoDB:

To create database in mongodb, we need a simple hack as it is not a simple straight forward command. The process is as below

1. Start mongo client using mongo 

2. Suppose  you want to create a database with name employee_db, use below command

It will show that database is switched to employee_db but it has not been created yet. You can confirm using command below.

It has not created the database yet, for that we need to insert a document in some collection only then database is created.

I hope this helped understand how to create database in mongodb. In upcoming articles, we will see about CRUD operations in mongoDB

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

2 thoughts on “create database in mongodb

  • Pingback:mongodb insert query - theJavaGeek

  • April 20, 2017 at 7:10 pm
    Permalink

    > db.employees.insert({name:”something”}
    WriteResult({ “nInserted” : 1 })
    > show dbs;
    employee_db 0.078GB
    local 0.078GB
    test 0.078GB

    bro just remove “s” from employees

    thanks

    Reply

Leave a Reply

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