Wednesday, 11 November 2015

MongoDB - Create/Drop Database

The use Command

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.

Syntax:
Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME
 
 
Example
>use krishnamongodb
switched to db krishnamongodb
 
 
To check your currently selected database use the command db
>db krishnamongodb
If you want to check your databases list, then use the command show dbs.
>show dbs 
local 0.78125GB 
test 0.23012GB

Your created database (krishnamongodb) is not present in list. To display database you need to insert atleast one document into it. To insert the document you need to create Collection first.

The dropDatabase() Method

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

Syntax:

Basic syntax of dropDatabase() command is as follows:
 
db.dropDatabase()

This will delete the selected database. If you have not selected any database, then it will delete default 'test' database

Example:

First, check the list available databases by using the command show dbs
>show dbs
local                0.78125GB
krishnamongodb       0.23012GB
test                 0.23012GB
>
 
If you want to delete new database <krishnamongodb>, then dropDatabase() command would be as follows:

>use krishnamongodb
switched to db krishnamongodb
>db.dropDatabase()
>{ "dropped" : "krishnamongodb", "ok" : 1 }
>
 
Now check list of databases
>show dbs
local      0.78125GB
test       0.23012GB
>



No comments:

Post a Comment

Monads in Scala

Monads belongs to Advance Scala   concepts. It  is not a class or a trait; it is a concept. It is an object which covers other object. A Mon...