MongoDB - Environment
Setting Up Your MongoDB Environment
Section titled “Setting Up Your 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.
Advantages of Atlas:
Section titled “Advantages of Atlas:”- 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.
Path 2: Local Installation
Section titled “Path 2: Local Installation”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).
Installation on Windows
Section titled “Installation on Windows”- Go to the official MongoDB Community Server download page.
- Select the MSI package for Windows.
- Run the installer. The recommended ‘Complete’ setup will install MongoDB as a Windows service, meaning it will start automatically when you boot your computer.
- The installer also gives you the option to install MongoDB Compass, a helpful GUI for your database. It is highly recommended to install it.
- During installation, ensure the ‘Add to System PATH’ option is checked. This allows you to run MongoDB commands from any terminal.
Installation on Linux (Ubuntu Example)
Section titled “Installation on Linux (Ubuntu Example)”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 Keywget -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 databasesudo apt-get update
# 4. Install the MongoDB packagessudo apt-get install -y mongodb-orgTo start, stop, and enable the MongoDB service, use systemd:
# Start the MongoDB servicesudo systemctl start mongod
# Verify the service is runningsudo systemctl status mongod
# Enable MongoDB to start on bootsudo systemctl enable mongodPath 3: The Universal Method - Docker
Section titled “Path 3: The Universal Method - Docker”Using Docker is an excellent, cross-platform way to run an isolated MongoDB instance for development.
# 1. Pull the official MongoDB imagedocker pull mongo
# 2. Run a container named 'my-mongo-db'docker run --name my-mongo-db -p 27017:27017 -d mongoConnecting 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 instancemongosh
# 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