The Wayback Machine - https://web.archive.org/web/20250118182650/https://www.geeksforgeeks.org/how-to-connect-to-mongodb-atlas-using-shell/
Open In App

How to Connect to MongoDB Atlas Using Shell?

Last Updated : 01 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MongoDB is a widely adopted NoSQL database, renowned for its ability to manage vast amounts of complex and unstructured data. Unlike traditional databases that use a tabular format, MongoDB employs a document-oriented approach, storing data in a flexible, JSON-like format. This makes MongoDB highly scalable and suitable for various modern applications.

In this article, We will learn about How to Connect to MongoDB Atlas Using Shell in detail.

MongoDB Atlas

  • MongoDB Atlas is a fully managed multi-cloud database.
  • It handles all the complexity of deploying and managing on cloud service providers like AWS, Google Cloud Platform and Microsoft Azure.
  • Atlas is a good way to deploy, run and scale MongoDB in the cloud.
  • MongoDB Atlas also has a free tier so we don’t need to pay anything to use the free plan.

MongoDB Shell

  • MongoDB Mongo shell is a JavaScript interface.
  • It allows us to easily interact with MongoDB instances through the command line.
  • It can be used to manipulate data stored in a database.
  • It allows users to manipulate and query data stored in MongoDB databases efficiently.

Steps to connect MongoDB Atlas using Shell

We need to follow the following steps to create a connection with MongoDB Atlas using Shell,

1. Install MongoDB on your local computer

After installing MongoDB. Now open any terminal and write the following command and check whether it is installed properly in your system or not.

mongod

Output:

Terminal 1
Starting MongoDB server on port: 27017

It starts the MongoDB server on the port 27017.

Now, keep this MongoDB Mongo window and open a new tab to run the Mongo shell that will connect to the MongoDB. Write the following command.

mongo

Output:

Terminal 2
Connecting the Mongo Shell to the local MongoDB Server

This connects your Mongo Shell to the local MongoDB server. Next, we'll connect the Mongo Shell to MongoDB Atlas.

2. Creating a Cluster in MongoDB Atlas

Step 1: Sign Up for MongoDB Atlas

Go to the MongoDB Atlas website and sign up for an account, You can also directly sign up with Google.

CreateS1

Step 2: Build a Database

Now, go to the database option and click on the "Build a Database" button.

Build a Database
Creating a Database

Step3: Configure Your Cluster

Choose the Free Cluster (M0). Select the cloud provider AWS, Google Cloud or Microsoft Azure for free hosting and then select the region in which it should be hosted. Rename the cluster if you want. At the end, click on the "Create" button to set up the free cluster.

Configure Your Cluster
Creating a Free M0 Cluster in MongoDB Atlas


CreateS
Configuring & Selecting the Hosting Region

Step 4: Set Up Access

Enter a username and password to create a user and network access to allow access anywhere. That will be the username and password through which the user can access the database to whom to give access.

Set Up Access
Creating an user for the Database

3. Establishing a Connection to the created Cluster from Shell

Step 1: Connect Using Mongo Shell

Click on "Connect" and then select "Connect with the MongoDB Shell". Then go to " I have the MongoDB Shell installed " option. Select the Mongosh version from there.

EstablishS1
Connecting to MongoDB using Shell

Step 2: Run the Connection Command

Copy the connection string and run the command in your terminal, Then you can enter the password that you set earlier with the username and password.

Run the Connection Command
Connection established with the MongoDB cluster via Mongo Shell

You are successfully connected with the MongoDB Atlas using Mongo shell. You are ready to write queries in the Mongo Shell and that will be reflected in the MongoDB Atlas cluster.

4. Writing Queries in Mongo Shell

Here are some basic queries to get you started:

Show Databases

 show dbs

Explanation: This command lists all the databases.

Create and Switch to a Database

use GFG

Explanation: This command creates and switches to the "GFG" database.

Insert a Document into a Collection

db. student.insertOne({"username": "arindam369",  "college": "JU"})

Explanation: It will create a collection "student" in the "GFG" database and insert a new JSON document in the "student" collection.

QueriesOutput
Queries

Output:

After executing the queries in the Mongo Shell, open the Collections in the MongoDB Atlas Cluster. We get the output given below.

Queries Cluster
After executing queries in the mongo shell, result in MongoDB Atlas

Conclusion

Connecting to MongoDB Atlas via the Mongo Shell provides a powerful and flexible environment for database management. This guide has walked you through setting up a cluster in MongoDB Atlas, configuring access, and connecting via the Mongo Shell. It also provided examples of basic queries to interact with your data. MongoDB Atlas, combined with the Mongo Shell offers a robust solution for managing and scaling databases across multiple cloud providers.

FAQs

What is MongoDB Atlas?

MongoDB Atlas is a fully managed, multi-cloud database service that automates the deployment, management, and scaling of MongoDB databases on cloud providers like AWS, Google Cloud, and Microsoft Azure.

How do I connect to a MongoDB Atlas cluster using the Mongo Shell?

To connect, you first create a cluster in MongoDB Atlas, set up access with a username and password, and then use the provided connection string in the Mongo Shell. The detailed steps include installing the MongoDB Shell, creating a cluster, and running the connection command in your terminal.

What is the purpose of the Mongo Shell?

The Mongo Shell is a command-line interface that allows users to interact with MongoDB databases. It enables data manipulation and querying through a JavaScript-based interface, making it easy to manage databases directly from the terminal.

Get IBM Certification and a 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

Master Data Analysis using Excel, SQL, Python & PowerBI with this complete program and also get a 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

three90RightbarBannerImg