You are already familiar with a database management system called MongoDB. Now it is time to talk about how to create a database in MongoDB and how to change databases while working. So, let's get started.
Creating the database via MongoDB Atlas
There are a couple of methods to create a Mongo database: using MongoDB Atlas or with the CLI (Command Line Interface).
Let's start with a more UI-friendly method – MongoDB Atlas. MongoDB Atlas is a user interface service for MongoDB databases management. Firstly, you need to create an account on the MongoDB Atlas website or log in via social networks. Secondly, you need to create a cluster:
Note, that you can create both free and paid clusters. MongoDB Atlas provides a free plan for the first cluster. Select the settings to your liking, choose the cluster name and click "Create Cluster". This could take some time, but with it you have created your first MongoDB cluster. Congrats!
Now, let's create a database itself. To do this, click on the cluster name that you have just created, then go to the "Collections" tab, then choose the option "Add My Own Data". This will open a modal window where you need to fill in the necessary fields and click on the "Create" button.
Now your database was created for you!
Creating the database via CLI
There is another way to create a database – using MongoDB command-line interface. Actually, there is no create command in the MongoDB shell to create a database, but you still can do it another way.
To start the shell you should use the mongosh command. In order to create a database, you need to switch your database to a non-existing one. Sounds strange, right? But it will create a database for you because the one you are trying to connect to does not exist at the moment. To do this, you just need to use a use command with any database_name you want as an argument:
> use database_name
/, \, |, ", $, *, <, >, :, ?
This is how you can create a database using MongoDB Shell. Let's take a look at what you need to do next when you have your own database.
Managing the databases
So, we have created a database but what's next? Now you will learn how to check your databases and switch between them.
To see all of your databases use show dbs command. Let's execute this command and see how it will look:
> show dbs
users 0.324GB
tasks 0.54GB
test_database 0.1265GB
As you can see we have 3 databases: users, tasks, and test_database. On the left side, we can see the database names, on the right side – the database weight in gigabytes.
Okay, now let's check our current database. To do this, just input the db command:
> db
users
So, let's switch to a different database that we have. Remember the use command from the previous paragraph? It is going to help us! Let's change our current database to tasks:
> use tasks
switched to db tasks
Amazing! Now we can work with the tasks database!
Conclusion
Congratulations! Now you know how to create a Mongo database and manage it. Let's sum up some key points:
- You can create a database via MongoDB Atlas or via MongoDB Shell.
- The
usecommand can help you create databases and switch between them. - You can see all of your databases using the
show dbscommand. - Also, use the
dbcommand to check your current database.
So, are you ready to use what you've learned in practice?