MongoDB - Create a database

MongoDB - Create a database

Command usage

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

Syntax

The basic syntax for using the DATABASE statement is as follows:

use DATABASE_NAME

example

If you want to use a database named <mydb> , use the DATABASE statement like this:

> use mydb
switched to db mydb

To check the database of your choice, use the db command

>db
mydb

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

>show dbs
local 0.78125GB
test 0.23012GB

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

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

In MongoDB, the database is a test database by default. If you haven't created a database, the collections will be stored in the test database.