Video Transcript (English)
Hey everyone! In this video, you’ll learn how to install the MongoDB Shell, also known as `mongosh`, in an Ubuntu environment and connect to an Atlas cluster.
The steps for installing `mongosh` are slightly different for each operating system. Refer to the resources included with this unit for instructions on installing `mongosh` on your local machine.
`mongosh` is a powerful shell for MongoDB. It gives users a friendly interface for managing, interacting with, and testing queries against your database. Let's get started!
The first step for installing `mongosh` on Ubuntu is to add the MongoDB repository. 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. After running these commands, we get a message that says OK.
Next, we need to create a new repository file for MongoDB by using this command:
Now that we added the MongoDB repository, we can install `mongosh` with this command:
sudo apt update
sudo apt install -y mongodb-mongosh
An easy way to check that `mongosh` was installed properly is to check the version that’s installed. We’ll do that now by running mongosh dash dash version.
Now that `mongosh` is installed, we’re ready to connect.
Connecting to an Atlas cluster requires the connection string for the cluster.
To connect to a cluster by using `mongosh`, we’ll paste a command copied from Atlas. To learn how to retrieve your connection string, check out the unit linked in the resources section.
Notice that this command contains a `–username` flag. This is one way to connect as a specific user in `mongosh`. We’ll replace the placeholder with the user that we created for our Atlas cluster.
Connection strings can also include the username and password in the string itself, which makes authentication easy. However, `mongosh` accepts connection strings that have authentication information included, as well as those that do not.
To connect to a local MongoDB instance running on the default port 27017, run `mongosh` in the terminal. This assumes we haven’t set up access control yet. If we don't specify a database, we'll be automatically connected to the `test` database.
What if we want to connect to a MongoDB instance that doesn’t use the default port?
`mongosh` allows us to specify a port by using the `--port` flag, like this:
Now let’s connect to our MongoDB Atlas cluster. After connecting, we can use the `db` helper to return the current database that we’re connected to. This is useful because it gives us an opportunity to ensure that we don't modify documents in the wrong database.
In this case, we’re connected to a database called `myFirstDatabase`, which is specified in the connection string provided to us by MongoDB Atlas.
Now we’ll 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 interact with our databases and collections by using `mongosh` helpers.
For example, we can view a list of all the collections in the current database by using the `show collections` helper. To get a list of all the databases on the server, we can use `show dbs`.
We can also perform CRUD operations on our data by using the `insertOne()`, `updateOne()`, and `deleteOne()` methods.
Furthermore, we can run various queries on our collections. For example, we can use the `find()` method to find all documents in a collection, or use `findOne() to find just a single document.
To exit mongosh, run the `exit` command. Great!
As a quick recap, in this video, we covered how to install `mongosh` on Ubuntu and how to connect to your Atlas cluster or local MongoDB instance. We also covered how to interact with your data in `mongosh`. For installation instructions for other operating systems, refer to the resources included with this unit.