How to connect to Mongo database using Mongo shell on Ubuntu?
Nam Hoang
Posted on January 29, 2024
Introduction
MongoDB, a widely used NoSQL database, provides developers with a flexible and scalable solution for managing large datasets. Connecting to a MongoDB database using the Mongo Shell is an essential skill for both developers and administrators. In this article, we will guide you through the step-by-step process of connecting to a MongoDB database using the Mongo Shell.
Prerequisites
To utilize the MongoDB Shell, you need a MongoDB deployment to connect to.
- For a free cloud-hosted deployment, consider using MongoDB Atlas.
- If you want to run a local MongoDB deployment, refer to the Install MongoDB documentation.
Supported MongoDB Versions:
You can use the MongoDB Shell to connect to MongoDB version 4.2 or greater.
Step 1: The following instructions are for Ubuntu 22.04 (Jammy).
-
Install gnupg and its required libraries:
sudo apt-get install gnupg
-
Retry importing the key:
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-7.0.asc
-
Create the
/etc/apt/sources.list.d/mongodb-org-7.0.list
file for Ubuntu 22.04 (Jammy):
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
-
Reload the local package database:
sudo apt-get update
-
Install the
mongosh
package:
sudo apt-get install -y mongodb-mongosh
-
Confirm that
mongosh
installed successfully:
mongosh --version
Step 2: Connect to MongoDB Atlas with mongosh
To establish your connection, run the mongosh
command with your connection string and options:
The connection string includes the following elements:
- Your cluster name
- A hash
- A flag for the API version
- A flag for the username you want to use to connect
It resembles the following string:
mongosh "mongodb+srv://YOUR_CLUSTER_NAME.YOUR_HASH.mongodb.net/" --apiVersion YOUR_API_VERSION --username YOUR_USERNAME
# for example
mongosh "mongodb+srv://db.fakeuri.mongodb.net/" --apiVersion 1 --username admin
Step 3: Run Commands
Switch Databases
To display the current database, use the following command:
db
This operation should return test
, which is the default database. To switch databases, use the use <db>
helper, as shown in the example below:
use <database>
Create a New Database and Collection
To create a new database, use the use <db>
command with the desired database name. For instance, the following commands create the myNewDatabase
database and the myCollection
collection using the insertOne()
operation:
use myNewDatabase
db.myCollection.insertOne( { x: 1 } );
If the collection does not exist, MongoDB creates it when you first store data for that collection.
Terminate a Running Command
To terminate a running command or query in mongosh, press Ctrl + C
. When you enter Ctrl + C
, mongosh:
- Interrupts the active command.
- Tries to terminate the ongoing, server-side operation.
- Returns a command prompt.
If mongosh cannot cleanly terminate the running process, it issues a warning.
Conclusion
Connecting to a MongoDB database using the Mongo Shell is a straightforward process that involves a few simple steps. Whether you're a developer building applications or an administrator managing databases, mastering the basics of the Mongo Shell is crucial for efficient MongoDB usage. Now that you've learned the essentials, you can explore further commands and functionalities to harness the full power of MongoDB for your projects.
Posted on January 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.