Video Transcript (English)
Welcome back, team! In this video, you’ll learn how to connect to your cluster via the MongoDB Shell by using your connection string.
You’ll also learn about the Node.js REPL environment that’s used by the MongoDB Shell.
To get started, log in to your Atlas account to access the project dashboard.
Once you’re logged in, navigate to the Database tab. This is where the clusters for the selected project are shown.
Next, click the Connect button.
On the next page, select the option to connect from the MongoDB Shell.
This will bring up instructions for installing the MongoDB Shell. Follow the instructions for your operating system. Or, if you already have the shell installed, skip to Step 3 and copy the entire command.
Next, open the command line, paste in the command, and press Enter to run it.
When prompted, enter the password you created when you made a database user in Atlas. If you forget your password, you can easily reset it in the Atlas dashboard under the Database Access tab.
After entering the password, press Enter.
Once we’re connected, we’ll get a message like this. It contains our mongosh shell log ID, the cluster we’re connected to, and the versions of MongoDB and the mongo shellmongosh that we’re using.
Now that we’ve successfully connected to the MongoDB Shell, let’s examine it in more detail.
The MongoDB Shell is a Node.js REPL environment. This gives us access to JavaScript variables, functions, conditionals, loops, and control flow statements right inside the shell.
Let’s observe this in action by writing some JavaScript code in the shell.
First, we’ll create a variable named const greetingArray.
Then we’ll assign the greetingArray variable to an array containing “hello”, “world”, and “welcome”.
Next, press Enter to begin a new line.
Now we’ll create another variable named const loopArray. We’ll set the new variable to a function that accepts an array and uses the forEach() method to loop through the array, logging each element in the console.
Finally, we’ll call the loopArray function and pass in the greetingArray as an argument.
When we press Enter, the code loops over the array and logs each element.
We can leverage these JavaScript features and more when writing queries in the MongoDB Shell.
Let’s review what we accomplished in this video:
First, we located the MongoDB Shell connection string.
Then, we connected to the MongoDB Shell by using the connection string.
Finally, we used the Node.js REPL environment to create JavaScript expressions in the MongoDB Shell.