How to Connect to MongoDB Atlas Using Shell?
Last Updated :
01 Aug, 2024
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:
Starting MongoDB server on port: 27017It 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:
Connecting the Mongo Shell to the local MongoDB ServerThis 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.

Step 2: Build a Database
Now, go to the database option and click on the "Build a Database" button.
Creating a DatabaseChoose 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.
Creating a Free M0 Cluster in MongoDB Atlas
Configuring & Selecting the Hosting RegionStep 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.
Creating an user for the Database3. 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.
Connecting to MongoDB using ShellStep 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.
Connection established with the MongoDB cluster via Mongo ShellYou 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:
After executing the queries in the Mongo Shell, open the Collections in the MongoDB Atlas Cluster. We get the output given below.
After executing queries in the mongo shell, result in MongoDB AtlasConclusion
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!
Similar Reads
How to Connect to MongoDB Atlas Using Shell?
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
5 min read
How to Connect Node.js To MongoDB Atlas Using Mongoose?
MongoDB Atlas is a cloud-based database service that offers robust features and scalability for managing our data. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple HTML form. In this tutoria
6 min read
How to use MongoDB atlas to save data ?
As modern applications require a database for storing data, MongoDB is a great choice. MongoDB Atlas is the global cloud database service for modern applications. We can use it for storing our application data on its server. Follows these steps for how to use MongoDB Atlas for saving data:Step 1: Vi
3 min read
How to Connect to a MongoDB Database Using Node.js
MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How to Connect Mongo DB with AWS using ATLAS?
The MongoDB Atlas provides the Application deployment solution by MongoDB hosting on AWS. MongoDB Atlas handles operational tasks such as backups, updates, and security configurations. There are many ways to connect MongoDB with AWS depending on specific use cases and requirements: MongoDB Atlas (Fu
5 min read
How to use MongoDB Connection String
MongoDB connection strings are essential for establishing connections between applications and MongoDB databases. These strings contain crucial information such as server addresses, authentication credentials and optional parameters, enabling seamless communication. Understanding the structure and c
4 min read
How to Get Data from MongoDB using Node.js?
One can create a simple Node.js application that allows us to get data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. Also, we use the EJS for our front end to render the simple HTML form and a table to show the data. Prerequisi
6 min read
How to drop database of MongoDB using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How To Use the MongoDB Shell
The MongoDB Shell, also known as mongosh is a powerful command-line interface that allows users to interact with MongoDB databases. It serves as a REPL (Read-Eval-Print Loop) environment by enabling users to execute commands, manage databases, and perform administrative tasks. In this article, We wi
5 min read
How to Connect to a MongoDB Database Using the Node.js Driver ?
MongoDB is a popular, open-source, NoSQL (non-relational) database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB stores a JSON-like format called BSON (Binary JSON). In this article, we connect the MongoDB database to your b
4 min read
How to Connect Node to a MongoDB Database ?
To connect node to a MongoDB database we can use mongoose library from npm, It is an Object Data Modeling Library that provides predefined methods for database connection, creating schema and models. MongoDB is a NoSQL database used to store large amounts of data without any traditional relational d
3 min read
Troubleshooting MongoDB Atlas Connection Errors
MongoDB Atlas is MongoDB's fully managed cloud service which offers high availability, automatic scaling and robust security for your databases. However, when connecting to MongoDB Atlas, we may encounter connection errors that can be difficult to detect and resolve. These errors can arise from netw
7 min read
How to Connect Mongodb Compass to Flask
An effective GUI tool for managing and visualizing MongoDB data is MongoDB Compass. On the other hand, a well-liked Python web framework for creating web apps is called Flask. Integrating your MongoDB data into your Flask web application may benefit from connecting MongoDB Compass to Flask. Through
2 min read
How to List All Collections in the MongoDB Shell?
Managing collections is a fundamental task in MongoDB database administration. Knowing how to list all collections in the MongoDB shell is essential for understanding your database structure and managing your data effectively. In this article, we'll explore how to list all collections in the MongoDB
3 min read
MongoDB - Create Database using Mongo Shell
A MongoDB Database is the container for all the collections, where the Collection is a bunch of MongoDB documents similar to tables in RDBMS and the Document is made up of fields similar to a tuple in RDBMS, but it has a dynamic schema here. This article explains the easy method to create a Database
2 min read
How to Retrieve Data from MongoDB Using NodeJS?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. Thi
3 min read
How to Get the Database URL in MongoDB
When working with MongoDB, it is important to know how to retrieve the database URL because as it enables applications to connect to the database. In this article, we'll explore how to obtain the database URL in MongoDB by covering various scenarios and providing examples. How to Get the Database UR
3 min read
How to Post Data in MongoDB Using NodeJS?
In this tutorial, we will go through the process of creating a simple Node.js application that allows us to post data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple
5 min read
How to create new Mongodb database using Node.js ?
mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project. Please r
1 min read