MongoDb Tutorial

Day 3 : Create Database in MongoDB








Mongodb tutorial series
Overview

In this part of the Learn Mongo Series, we will learn how to create a database in mongodb. We will be creating a database with the name demo_db .

Let's create a database

Well , you will find it strange but there is no command to create a database in mongodb however we can start using a database in mongodb. And when we start using a database , it will be automatically created. Let's go through the whole process step by step.

  • Step 1 - Have a look : Before proceeding any further let's have a look at the previously created databases in your DB with the show dbs command as shown below :

    										
    > show dbs
    										
    									

    output of the above command will display all the available non-empty mongodb databases in your system.

    										
    > show dbs
    activityoverload        0.203125GB
    animals 0.203125GB
    clonecs 0.203125GB
    local   0.078125GB
    test    0.203125GB
    >
    										
    									

  • Step 2 - Start using your DB : As already explained above, there is no command in mongodb to specifically create a database, so we can create it by start using it. We can start using a database using the use <database_name> command as shown below :

    										
    > use demo_db
    										
    									

    output of the above command will display the following :

    										
    > use demo_db
    switched to db demo_db
    
    >
    										
    									

  • Step 3 - Insert some data : Although our database is created successfully with step-2 but we can not see it in show dbs commands list of databases displayed below :

    										
    > show dbs
    										
    									

    output of the above command will display the following :

    										
    > show dbs
    activityoverload        0.203125GB
    animals 0.203125GB
    clonecs 0.203125GB
    local   0.078125GB
    test    0.203125GB
    >
    	
    										
    									

    This is because show dbs command does not display empty databases. SO we need to add some content in our database using the following command
    db.<collection_name>.insert({"key":"value"}) as shown below :
    Note : insert command is explained in detail in the coming tutorials, so don't worry if you don't understand it now.

    										
    > use demo_db
    switched to db demo_db
    
    > db.details.insert(
    ...{
    ...  "Website":"nodejsera"
    ...}) 
    
    >show dbs
    	
    	
    										
    									

    After inserting the data, we are also using the show dbs which will produce the following output :

    										
    > show dbs
    activityoverload        0.203125GB
    animals 0.203125GB
    clonecs 0.203125GB
    demo_db 0.203125GB
    local   0.078125GB
    test    0.203125GB
    >
    										
    									

  • Hurray !! Mission Accomplished. We have successfully created a database in mongodb.

Summary

In this part of learn mongo series , we learned about how we can create a database in mongodb. We learned the following commands of mongodb :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. show dbs : This command is used to list all the non-empty mongodb databases on the console