Skip to content

MongoDB - Environment

This guide covers the modern and recommended ways to get started with MongoDB, from a zero-install cloud option to local installations on Windows, macOS, and Linux.

Path 1: The Easiest Start - MongoDB Atlas (Cloud)

Section titled “Path 1: The Easiest Start - MongoDB Atlas (Cloud)”

For beginners and production applications alike, the fastest way to start is with MongoDB Atlas, the official managed cloud database. You get a free-tier cluster that is perfect for learning and small projects.

  • No Installation: Get a running MongoDB instance in minutes.
  • Managed for You: Atlas handles setup, backups, security, and scaling.
  • Multi-Cloud: Deploy on AWS, Google Cloud, or Azure.
  • Includes Tools: Access your data via the Atlas UI, a built-in shell, and a connection string for your applications.

To start, visit the MongoDB Atlas website, sign up, and follow the on-screen instructions to create a free M0 cluster. Once created, you will get a connection string that you can use with mongosh or your application driver.

If you prefer to run MongoDB on your local machine for development, follow the instructions for your operating system. You will need to install two key components: MongoDB Community Server and MongoDB Shell (mongosh).

  1. Go to the official MongoDB Community Server download page.
  2. Select the MSI package for Windows.
  3. Run the installer. The recommended ‘Complete’ setup will install MongoDB as a Windows service, meaning it will start automatically when you boot your computer.
  4. The installer also gives you the option to install MongoDB Compass, a helpful GUI for your database. It is highly recommended to install it.
  5. During installation, ensure the ‘Add to System PATH’ option is checked. This allows you to run MongoDB commands from any terminal.

Follow the official documentation for your specific Linux distribution. For a recent Ubuntu LTS version, the steps are as follows:

# 1. Import the MongoDB public GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
# 2. Create a list file for MongoDB (check official docs for your specific Ubuntu version)
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
# 3. Reload local package database
sudo apt-get update
# 4. Install the MongoDB packages
sudo apt-get install -y mongodb-org

To start, stop, and enable the MongoDB service, use systemd:

# Start the MongoDB service
sudo systemctl start mongod
# Verify the service is running
sudo systemctl status mongod
# Enable MongoDB to start on boot
sudo systemctl enable mongod

Using Docker is an excellent, cross-platform way to run an isolated MongoDB instance for development.

# 1. Pull the official MongoDB image
docker pull mongo
# 2. Run a container named 'my-mongo-db'
docker run --name my-mongo-db -p 27017:27017 -d mongo

Connecting with the MongoDB Shell (mongosh)

Section titled “Connecting with the MongoDB Shell (mongosh)”

Once your server is running (locally, in Docker, or on Atlas), you can connect to it using mongosh, the modern command-line interface.

# Connect to a local instance
mongosh
# Connect to any instance using a connection string (e.g., from Atlas)
mongosh "mongodb+srv://<username>:<password>@cluster0.abcde.mongodb.net/myFirstDatabase"

Once connected, you can run commands. Here are some basics:

> show dbs // List all databases
> use myNewDb // Switch to or create a new database
> show collections // List collections in the current database
> db.stats() // Get statistics for the current database