While graphical tools like MongoDB Compass are fantastic for visualizing data, true MongoDB mastery requires you to understand the command line.
In this module, we will introduce you to mongosh (the MongoDB Shell). This is where you will write scripts, automate database tasks, and execute all the CRUD operations we will learn in the upcoming modules.
Introduction to mongosh
mongosh (short for MongoDB Shell) is a fully functional JavaScript and Node.js environment that runs in your terminal. It allows you to interact directly with your MongoDB database using JavaScript commands.
Because mongosh is built on Node.js, you can write actual JavaScript code right in your terminal. For example, if you wanted to generate an array of numbers, you could literally type standard JS arrays and loops directly into the database shell!
(Note: In older tutorials from before 2020, you might see people using the mongo command. That is the legacy shell. It has been completely replaced by the modern, far more powerful mongosh.)
Starting the Shell and Connecting
To start the shell, you need to open your computer’s terminal (Command Prompt or PowerShell on Windows; Terminal on macOS/Linux).
Connecting to a Local Database
If you installed MongoDB locally and the database service is running in the background, connecting is incredibly simple. Just type:
mongosh
Because you didn’t provide a specific address, mongosh assumes you want to connect to a local database running on the default port (localhost:27017).
Connecting to MongoDB Atlas (Cloud)
If you are using the free cloud database we set up in Module 2, you need to provide the shell with your unique connection string.
- Go to your MongoDB Atlas dashboard.
- Click Connect on your cluster.
- Select Connect with the MongoDB Shell.
- Copy the connection string provided. It will look something like this:
mongosh "mongodb+srv://cluster0.example.mongodb.net/myFirstDatabase" --apiVersion 1 --username myUser
Paste that into your terminal and press Enter. It will prompt you for your password. Note: When you type your password, the characters will remain invisible for security reasons. Just type it and press Enter!
Basic Shell Commands
Once connected, your terminal prompt will change to show the database you are currently in, which is usually test> by default.
Let’s learn the essential commands for navigating your database server.
1. Show all Databases
To see a list of all databases on your server, use:
show dbs
You will likely see default administrative databases like admin, config, and local.
2. Switch to a Database
To create a new database or switch to an existing one, use the use command followed by the database name:
use school_db
Note: MongoDB will not actually create school_db on your hard drive until you insert data into it. It is created “lazily.”
3. Check Current Database
If you forget which database you are currently working inside, just type:
db
This will print the name of the active database (e.g., school_db). In mongosh, the word db always refers to your currently selected database.
4. Show Collections
To see all the collections (tables) inside your current database, type:
show collections
5. Clear the Screen
If your terminal gets too messy, you can clear it by typing:
cls
(You can also use the standard terminal shortcut Ctrl + L).
The Help Command
You don’t need to memorize every single command in MongoDB. The shell has excellent built-in documentation.
If you ever get stuck or want to see what operations are available on a specific database or collection, simply type .help at the end of the object.
To see all commands you can run on the database:
db.help()
To see all commands you can run on a specific collection (e.g., students):
db.students.help()
This will print out a massive list of commands like find(), insertOne(), deleteMany(), and more, explaining exactly what they do.
Exit Commands
When you are finished working in the database and want to return to your normal computer terminal, you need to exit the shell gracefully.
You have three ways to do this:
- Type
exitand press Enter. - Type
quitand press Enter. - Press
Ctrl + Ctwice on your keyboard.
Summary
You now know how to navigate the MongoDB ecosystem from the command line!
- You used
mongoshto connect to your database. - You used
show dbsanduse <database>to navigate around. - You learned that the
dbkeyword represents your current database.
In Module 5, we are going to put all of this to use. We will use the shell to perform the four core operations of any application: Create, Read, Update, and Delete (CRUD) operations!
Discussion
Loading comments...