Lesson 1: Installing and Connecting to the MongoDB Shell / Learn

Code Summary: Installing and Connecting to the MongoDB Shell

The following code demonstrates how to install and use mongosh in a Linux environment.

  1. Update apt and install gnupg. Then add the MongoDB public GPG key to the system.
  2. apt update 
    apt install <code>gnupg</code>
    wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - 

  3. Create a list file for MongoDB.
  4. echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu 
    focal/mongodb-org/6.0 multiverse" | sudo tee 
    /etc/apt/sources.list.d/mongodb-org-6.0.list

  5. Update apt and install mongosh.
  6. apt update
    apt install -y mongodb-mongosh

  7. Check that mongosh is installed.
  8. mongosh --version
    

  9. Exit mongosh.
  10. exit

Connect to Your Atlas Cluster by Using mongosh

The following code demonstrates how to connect to your MongoDB Atlas cluster by using mongosh.

  1. Run the mongosh command followed by your connection string.
  2. mongosh "mongodb+srv://<username>:<password>@<cluster_name>.example.mongodb.net"

    Alternatively, you can log in by providing the username and password as a command line argument with -u and -p.

    mongosh -u exampleuser -p examplepass "mongodb+srv://myatlasclusteredu.example.mongodb.net"

  3. After connecting to the Atlas cluster, run db.hello(), which provides some information about the role of the mongod instance you are connected to.
  4. db.hello()

  5. Finally, run the exit command inside mongosh to go back to the terminal.
  6. exit