English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Create a Database in MongoDB

In this chapter, we will see how to create a database in MongoDB.

Use the command

use DATABASE_NAME Used to create a MongoDB database. This command will create a new database (if it does not exist), and if the database exists, it will return the existing database.

Syntax

use DATABASE The basic syntax of the statement is as follows-

use DATABASE_NAME

Online Examples

If you want to use a database with name<mydb>thenuse DATABASEThe statement is as follows:

>use mydb
switched to db mydb

To check the database you are currently using, use the following command db

>db
mydb

To check the list of databases, use the following command show dbs.

>show dbs
local     0.78125GB
test      0.23012GB

The database (mydb) you created is not on the list. To display the database, you need to insert at least one document.

>db.movie.insert({"name":"oldtoolbag.com")
>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB

By default, the default database in MongoDB is test. If you have not created any database, the collection will be stored in the test database.