How to connect to Mongo database using Mongo shell on Ubuntu?
Tags: mongodb, terminal, ubuntu, mongodbcrashcourse
Published: 2024-01-29 20:56:33

# 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](https://www.mongodb.com/cloud/atlas).
- If you want to run a local MongoDB deployment, refer to the [Install MongoDB](https://www.mongodb.com/docs/manual/installation/) 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:

    ```sh
    sudo apt-get install gnupg
    ```

2. Retry importing the key:

    ```sh
    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):

    ```sh
    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:

    ```sh
    sudo apt-get update
    ```

5. Install the `mongosh` package:

    ```sh
    sudo apt-get install -y mongodb-mongosh
    ```

6. Confirm that `mongosh` installed successfully:

    ```sh
    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:

```sh
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:

```sh
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:

```sh
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:

```sh
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.