Video Transcript (English)
Welcome back, everyone! Now that you know where to locate your connection string so that you can connect to your cluster, let’s shift our focus to the MongoDB Shell.
In this video, you’ll learn how to install the MongoDB Shell, also known as mongosh. We'll install it in an Ubuntu environment and connect to an Atlas cluster.
Before we begin, it’s important to keep a couple of things in mind.
First, certain editions and platforms of MongoDB server installations come with mongosh included and installed alongside the server. If you’ve installed MongoDB locally and are unsure whether mongosh is included, check your system or the documentation for your installation.
Second, steps for installing mongosh are slightly different for each operating system. Refer to the MongoDB documentation for instructions on installing mongosh in your specific environment.
Alright, let’s get started.
Mongosh is a powerful shell for MongoDB. It gives users a friendly interface for testing queries, managing and interacting with your databases.
To install `mongosh` on Ubuntu, we first need to add the MongoDB repository by using these commands.
To do this, we’ll update the package list and install `gnupg`, which is a required package for `mongosh`.
Finally, we’ll add MongoDB's public key. Running this command writes the GPG key to the correct folder and prints the key to the terminal.
Next, we’ll add the remote repository for MongoDB to the apt sources list directory. This lets the package manager know where to fetch the packages for installing MongoDB on our system.
Now that we added the MongoDB repository to the package manager configuration, we can run sudo apt update, and then we can install mongosh with this command.
An easy way to check that mongosh was installed properly is to check the version. We’ll do that now by running mongosh dash dash version.
Now that we've confirmed that mongosh was installed, we’re ready to connect.
Let's take a moment to talk about how to connect to a MongoDB deployment running locally.
By default, MongoDB instances run on the default port 27017.
However, you may find that you are running MongoDB on a server that already has a process using that port. Since we've already connected to Atlas using the default port 27017, let's see how to connect to a MongoDB instance running locally using a different port. Here we'll use the port 28888 for our example.
Let's invoke the mongosh command with the [hyphen][hyphen] port option followed by the port number.
We see the command in action connecting to a local MongoDB instance running on port 28888.
To connect to an Atlas cluster, we need the connection string for that cluster.
If you’re unsure how to locate your connection string, check out our previous video on connection strings.
When we run it, we're prompted to input our password to log in.
Remember, to log in on your own cluster, you'll need to use your own connection string and credentials.
That worked!
Now that we're connected, we can interact with our databases and collections by using mongosh helpers.
Let's use the `db` helper to return the current database that we’re connected to. This is useful because it’s easy to make sure we're in the right database before modifying documents.
Now let's use the `use` helper to change databases. For example, if we want to switch to the `sample_training` database, we would run the following.
We can also view a list of all the collections in the current database with the `show collections` helper.
To get a list of all the databases on the server, we use `show dbs`.
To see the list of commands available in the mongosh console, type help.
mongosh provides numerous other methods and commands you can use to get help using mongosh and information about your deployment. Check out our documentation for more details on these.
To exit mongosh, run the `exit` command. Great!
Nice!
Let’s recap what you learned in this video.
First, we covered how to install mongosh on Ubuntu and connect to your Atlas cluster.
We’ll explore other ways to use mongosh in an upcoming lesson.
For installation instructions for other operating systems, refer to the MongoDB documentation.
But don’t go away, because there’s more to learn about connecting to your databases, as well as the MongoDB Shell. In the next video, we’ll take a look at troubleshooting connection errors.