MongoDB Tutorial - MongoDB Create Database








MongoDB's use DATABASE_NAME command 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

Run mongo.exe to start the client.

To create a database with name mydb, then use DATABASE statement would be as follows:

>use mydb
switched to db mydb

To check your currently selected database use the command db

>db
mydb

To check your databases list, then use the command show dbs.

>show dbs
local     0.72222GB
test      0.22222GB

Empty database (mydb) is not present in list. To display database, insert some data into it.

>db.movie.insert({"name":"java2s.com"})
>show dbs
local      0.72225GB
mydb       0.22222GB
test       0.23012GB

In mongodb default database is test. If you didn't create any database then collections will be stored in test database.