You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Before you can run any MongoDB commands you need to install the MongoDB Community Server and the mongosh shell on your local machine. This lesson walks through installation on macOS and Ubuntu Linux.
Homebrew is the easiest way to install MongoDB on a Mac.
# Add the MongoDB tap
brew tap mongodb/brew
# Install the latest MongoDB Community Server
brew install mongodb-community
# Start MongoDB as a background service
brew services start mongodb-community
To verify the server is running:
brew services list | grep mongodb
# Import the MongoDB public GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
# Add the MongoDB repository
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Install
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start and enable the service
sudo systemctl start mongod
sudo systemctl enable mongod
mongosh is the modern MongoDB shell that replaces the legacy mongo shell.
# macOS
brew install mongosh
# Verify installation
mongosh --version
On Linux, mongosh is included in the mongodb-mongosh package, which is installed automatically with the steps above.
Once the server is running, open mongosh:
mongosh
You will see a prompt like test>. The default database is test. You can switch databases with the use command:
use myapp
If you prefer not to install locally, MongoDB Atlas offers a free M0 cluster in the cloud. Sign up at cloud.mongodb.com, create a free cluster, whitelist your IP, create a database user, and copy the connection string. Then connect with mongosh:
mongosh "mongodb+srv://cluster0.example.mongodb.net/myapp" --username myuser
Inside mongosh you can verify the server is healthy:
db.adminCommand({ serverStatus: 1 })
With MongoDB installed and running, you are ready to start inserting documents in the next lesson.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.