How to create, use and drop database in mongoDB.

In this tutorial I will explain you how to create, use and drop database in mongoDB. Once you are done with mongo database installation. Make sure to install latest version always. For installation try this How to install up MongoDB and PHP on Ubuntu

Now start mongoDB. Run this command

sudo service mongodb start

MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it doesn’t exist otherwise it will return the existing database.

Basic syntax of use DATABASE statement is as follows:

use DATABASE_NAME

Now lets create a database with name ‘mydatabase’

first start the mongoDB with command

sudo service mongodb start

now run the command mongo

Now create the database run this command

>use mydatabase

You will see this output

switched to db mydatabase.

To check your currently selected database run this command

>db

you will see this output

mydatabase

TO check all database list run this command

>show dbs

Your created database will not be shown in this list till you will not insert the data.

now run this command

>db.tablename.insert({"name":"w3code"})
>show dbs
local            0.78125GB
mydatabase       0.23012GB

Now you will se the your table name in the output.

MongoDB db.dropDatabase() command is used to drop a existing database.

First you need to switch to the database youwant to drop and then run the command

>use mydatabase
switched to db mydatabase
>db.dropDatabase()
>{ "dropped" : "mydatabase", "ok" : 1 }

Now check the list of the database you will not see the database you have droped(mydatabase).
Hope this tutorial how to create, use and drop database in mongoDB will help you.