MongoDB is one of the easiest databases to get started with! MongoDB can easily be run in a terminal using the MongoDB Shell (mongosh
for short). Throughout this course, we will be providing you with your very own mongosh
shell via a terminal. Now - before we can get into making fancy queries on our data, one of the first things we will have to do is navigate around our database instances. MongoDB allows us to store multiple databases inside of a single running instance.
For example, imagine we are a freelance developer using MongoDB to manage the data for multiple different projects: an e-commerce shop, a social media application, and a portfolio website. To compartmentalize our data, we can create a separate database for each project.
With all these databases in our MongoDB instance, how exactly would we choose and navigate around them? Fortunately, MongoDB offers us some handy commands to easily see a list of all our databases, switch databases, and confirm which database we are currently using.
First, let’s list all of our existing databases for our freelance projects. To see all of our databases, we can run the command show dbs
. This will output a list of all the databases in our current instance and the disk space each takes up. Here is what it might look like:
online_plant_shop 73.7 KiB plant_lovers_meet 55.7 MiB my_portfolio_site 9.57 MiB admin 340 KiB local 1.37 GiB config 12.00 KiB
Looking at the example output above, notice three unique databases: admin
, config
, and local
. These databases are included by MongoDB to help configure our instance. In addition, we have our three databases for each of our freelance projects.
Note: We won’t be working with the
admin
,config
, andlocal
databases throughout this course, but feel free to explore them on your own!
Now that we have a full list of our databases in our MongoDB instance, we will need to choose the specific one we want to work with. To navigate to a particular database, we can run the use <db>
command. For example, if we wanted to use our e-commerce database, we’d run use online_plant_shop
. This would place us inside our online_plant_shop
database, where we have the option to view and manage all of its collections. It’s important to note, that if the database we specify does not exist, MongoDB will create it, and place us inside of that database.
Here is what our terminal might look like:
Notice that the terminal will list the current database we are in before a >
symbol. When we switch databases, we should see the name of the database we switched into displayed there instead. In this case, we can see the prompt changed from test>
to online_plant_shop>
.
If at any point we lose track of what database we are in, we can orient ourselves by running the command, db
. This will output the name of the database we are currently using. It would look like this:
Now that we have covered the basics, let’s practice navigating a MongoDB instance!
Instructions
Let’s get familiar with our environment and orient ourselves by seeing what databases currently exist in our database instance.
Use the appropriate MongoDB command to see a list of all the current databases.
To check your commands for each task, use the Check Work button.
Note: Every exercise in this course will have the MongoDB Shell connected to a
test
database when it first loads.
Throughout this course, we will be working closely with the restaurants
database. Navigate to the restaurants
database in your MongoDB Shell.
Before moving on, use the appropriate MongoDB command to confirm that you are in the restaurants
database.