Home >>MongoDB Tutorial >MongoDB Create Database

MongoDB Create Database

How to Create Database using MongoDB

The use Command

To build databases, MongoDB uses DATABASE_NAME. If it doesn't exist, the command will create a new database, otherwise it will return the original database.


Basic syntax of use DATABASE statement is as follows −


If you want to use 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


If you want to check your list of databases, use the show dbs command.

>show dbs
local     0.78125GB
test      0.23012GB

The database you created (mydb) is not mentioned. You need to insert at least one document into it in order to view the database.

>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB

The default database in MongoDB is Test. If no database has been established, then the collections are stored in the test database.

No Sidebar ads