MongoDB Setup
Written by Kashif NazirBelow 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
- Download the MongoDB installer, selecting the "current release" version, your OS, and "MSI" for the package.
- 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.
- Also download the
mongosh
zip file and extract it to an easily accessible location. You should see abin
folder, and inside that there should bemongosh.exe
. - To open up your MongoDB shell, you should be able to double click
mongosh.exe
. - You will also need to be able to run
mongosh.exe
from PowerShell, by runningPATH_TO_MONGOSH_FOLDER\bin/mongosh.exe
, replacingPATH_TO_MONGOSH_FOLDER
with the full path where you extracted the zip. - Follow the steps to verify your setup.
Instructions for macOS
- From the terminal tap the MongoDB Homebrew tap by running
brew tap mongodb/brew
. - Install MongoDB through homebrew by running
brew install mongodb-community@6.0
. - Create a directory to store your MongoDB database(s) for the future (e.g.
mkdir ~/MongoDB
). - Run the MongoDB server with the command
mongod --dbpath PATH_TO_YOUR_DB_DIRECTORY
(replacingPATH_TO_YOUR_DB_DIRECTORY
with the path from the previous step). You need to keep this window open while the server is running. - To open a MongoDB shell, run
mongosh
in another terminal window. - Follow the steps to verify your setup.
Instructions for Linux
This depends heavily on your Linux distro. Here are the instructions for Ubuntu:
- Import the MongoDB public GPG keys via
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
. - 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
(replacingRELEASE
with your Ubuntu release name, e.g. "focal"). - Reload your local package database through
sudo apt update
. - Install the MongoDB package with
sudo apt install mongodb-org
. - Start the
mongod
service by runningsudo systemctl start mongod
. - 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
. - To open the MongoDB shell, run
mongosh
. - Follow the steps to verify your setup.
Verify Your Setup (macOS/Linux)
- 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.
- Start
mongosh
as described in your operating system's instructions. - You should be prompted with a MongoDB shell that lets you type in commands. In this case, enter
use example
. - 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. - 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. - You can stop
mongosh
by typingquit
. You don't need to havemongosh
open while working, but you can if you want to confirm the contents of your database. - 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.