MongoDB Setup

Written by Kashif Nazir

Below you'll find information on how to set up MongoDB. This page assumes that you have successfully completed the setup from assignmnent 0 to install Node on your machine as well as Homebrew for macOS users.

Contents

Instructions for Windows

  1. Download the MongoDB installer, selecting the "current release" version, your OS, and "MSI" for the package.
  2. Run the installer. Make sure to select the complete installation and check "Install MongoD as a Service." This should let the server stay running without you explicitly having to start it.
  3. Also download the mongosh zip file and extract it to an easily accessible location. You should see a bin folder, and inside that there should be mongosh.exe.
  4. To open up your MongoDB shell, you should be able to double click mongosh.exe.
  5. You will also need to be able to run mongosh.exe from PowerShell, by running PATH_TO_MONGOSH_FOLDER\bin/mongosh.exe, replacing PATH_TO_MONGOSH_FOLDER with the full path where you extracted the zip.
  6. Follow the steps to verify your setup.

Instructions for macOS

  1. From the terminal tap the MongoDB Homebrew tap by running brew tap mongodb/brew.
  2. Install MongoDB through homebrew by running brew install mongodb-community@6.0.
  3. Create a directory to store your MongoDB database(s) for the future (e.g. mkdir ~/MongoDB).
  4. Run the MongoDB server with the command mongod --dbpath PATH_TO_YOUR_DB_DIRECTORY (replacing PATH_TO_YOUR_DB_DIRECTORY with the path from the previous step). You need to keep this window open while the server is running.
  5. To open a MongoDB shell, run mongosh in another terminal window.
  6. Follow the steps to verify your setup.

Instructions for Linux

This depends heavily on your Linux distro. Here are the instructions for Ubuntu:

  1. Import the MongoDB public GPG keys via wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -.
  2. Create a list file for MongoDB via echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu RELEASE/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list (replacing RELEASE with your Ubuntu release name, e.g. "focal").
  3. Reload your local package database through sudo apt update.
  4. Install the MongoDB package with sudo apt install mongodb-org.
  5. Start the mongod service by running sudo systemctl start mongod.
  6. You will need to start the service each time you want to run the server. Alternatively, you can start the server on reboot by running sudo systemctl enable mongod.
  7. To open the MongoDB shell, run mongosh.
  8. Follow the steps to verify your setup.

Verify Your Setup (macOS/Linux)

  1. Make sure your MongoDB server is running. Whenever you want to interact with a MongoDB database, you'll have to make sure it's running with instructions depending on your operating system.
  2. Start mongosh as described in your operating system's instructions.
  3. You should be prompted with a MongoDB shell that lets you type in commands. In this case, enter use example.
  4. Next, insert a new document into the database through db.myCollection.insertOne({ binky: "winky" }). If successful, you should see some sort of acknowledgement that the document was inserted.
  5. After the document has been inserted, retrieve it using db.myCollection.find(). You should see the result returned from the database containing the object you just inserted.
  6. You can stop mongosh by typing quit. You don't need to have mongosh open while working, but you can if you want to confirm the contents of your database.
  7. Congratulations! MongoDB should be successfully installed and set up. Remember: You need to make sure the MongoDB server (mongod) is running whenever working with an application that is trying to connect to it (i.e. your assigment/project).

Additional Resources

The guide here is an abbreviated version of the official MongoDB installation instructions. For extra troubleshooting tips, information for other Linux distros, etc., please check the linked guide for your operating system or post on the forum.