How to use MongoDB create database

There isn’t actually command called MongoDB Create Database. However, you can still create databases with one simple trick. All you have to do is enter the keyword “use” and the system will automatically create a new database.

What is MongoDB Create Database?

MongoDB sets itself apart from the relational Database Management System MySQL by not using tables. MongoDB has chosen to focus on maximum scalability and flexibility instead. The NoSQL solution works with databases and allows users to store important information or edit as needed. However, the classic command Create Database does not actually exist in MongoDB. In this tutorial, we’ll show you how to create databases without the MongoDB command.

What is the syntax and functionality of MongoDB Create Database?

It will be easier to understand exactly how this works once you take a look at the syntax and functionality of the MongoDB Create Database function. The structure is always the same:

use DATABASE_EXAMPLE

The keyword “use” replaces the Create Database command. The system switches from one database to the next once it has been entered. However, the system will create a new database called “DATABASE_EXAMPLE” if there isn’t a database with this exact name. The output will look like this:

use DATABASE_EXAMPLE
switched to DATABASE_EXAMPLE

Keep in mind that MongoDB does not officially recognize this database yet. While a database with this name exists, it is not considered by the system since it’s empty. The software will accept and create the new database when you create a new collection with MongoDB Create Collection or add the first documents. MongoDB doesn’t need a schema and it allows users to use different data types without a predefined structure.

Tip

Do you want to learn more about the Database Management System MongoDB? Or would you like to learn how to add a new user or create an index? Take a look at our other Digital Guide articles on these topics:
- MongoDB Create User
- MongoDB Create Index

How do I check all existing databases?

Firstly, you should check which databases already exist before creating a new one. Use the following command to do this:

show dbs

This will list all the current databases. Use the following command if you’re not sure which database you currently have selected:

db

What are the default databases?

When you first install MongoDB, the system will automatically create three different default databases. Decide if the existing databases “Admin”, “Config” and “Local” are enough for you before using Create Database in MongoDB.

What is an example of MongoDB Create Database?

We’ll show you a simple example of how Create Database works in MongoDB to give you a better understanding of how it works. Let’s assume that you want to create a new database called “customers”. We’ll proceed step by step. Start by querying which databases exist:

>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
apacheconf

Use the MongoDB function Create Database and create a new database using the command “use” with the desired name:

> use customers
switched to customers
>

You can query it again if you want to make sure you are using the correct database:

> db
customers
>
apacheconf

If you are requesting a new listing now, the new database would still not show up because it is still empty. That means you need to add at least one document and request the listing:

>show dbs

admin 0.847GB

config 0.347GB

local 0.272GB

customers 0.182GB
php
Tip

Now Create Database in MongoDB, but are you still wondering how to remove databases that you no longer need? Our Digital Guide includes an article on MongoDB Drop Database, as well as a comprehensive MongoDB Tutorial to get you up to speed.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.