How to Create Database in MongoDB?
In MongoDB, the following syntax is used to create a new database:
In the above-given syntax, Database_Name is the name of the Database you want to create.
Moving ahead, MongoDB also provides you with a feature to check all Database lists present. Follow the syntax below for the same:
Giving a start to the MongoDB tutorial, let's first check out what all databases already exist:
To create another Database named MyDB, follow the code below:
You can notice, that Database named MyDB is now created. However, if again we try to run the show dbs command to check the Database list, we will get the following output:
1)The above list is not displaying the recently created MyDB Database. Well, the answer to this is, MyDB database is the one that contains no collection. This is the reason why this database is not displayed in the above list.
2) While creating a Database, if Database_Name matches the Database that already exists, then, no new Database with that name will be created instead the current Database will be switched to the same.