Create Database in MongoDB

The use DATABASE_NAME command in MongoDB is used to create the database. This command will create a new database, if it does not exist yet, otherwise, this command will return the existing database.

Use command in MongoDB

The use DATABASE_NAME command in MongoDB is used to create the database. This command will create a new database, if it does not exist yet, otherwise, this command will return the existing database.

Syntax

The basic syntax of the use DATABASE_NAME command is as follows:

 use DATABASE_NAME 

For example

If you want to create a database named mydb , you use the use DATABASE command as follows:

 > use mydb switched to db mydb 

To check the currently selected database, you use the db command.

 > db mydb 

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

 > show dbs local 0.78125GB test 0.23012GB 

Your created mydb database is not on this list. To display it, you need to insert at least one Collection into it.

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

In MongoDB, the default database is test. If you have not created any databases, then the Collection will be stored in the test.

According to Tutorialspoint

Previous article: Modeling data in MongoDB

Next lesson: Delete Database in MongoDB

4 ★ | 1 Vote