DEV Community

Nam Hoang
Nam Hoang

Posted on

How to connect to Mongo database using Mongo shell on Ubuntu?

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).

  1. Install gnupg and its required libraries:

    sudo apt-get install gnupg
    
  2. 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
    
  3. 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
    
  4. Reload the local package database:

    sudo apt-get update
    
  5. Install the mongosh package:

    sudo apt-get install -y mongodb-mongosh
    
  6. 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
Enter fullscreen mode Exit fullscreen mode

Step 3: Run Commands

Switch Databases

To display the current database, use the following command:

db
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 } );
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)