Installing MongoDB and Using Atlas

beginner
12 min

Installing MongoDB and Using Atlas

Before you can practice queries, you need a running MongoDB server. In this lesson you will learn two ways to get one: installing MongoDB Community Server on your own machine, and creating a free cloud database with MongoDB Atlas. You will also meet mongosh, the modern MongoDB shell that we will use throughout this CodeYourCraft tutorial series.

Option 1: Install MongoDB Community Server Locally

MongoDB Community Server is free and runs on Windows, macOS, and Linux.

Windows: download the MSI installer from the official MongoDB download center, choose the Complete setup, and let the installer register MongoDB as a Windows service so it starts automatically. The installer can also add MongoDB Compass, the official GUI.

macOS: the easiest path is Homebrew:

bash
brew tap mongodb/brew brew install mongodb-community@7.0 brew services start mongodb-community@7.0

Linux (Ubuntu): add the official MongoDB APT repository, then install and start the service:

bash
sudo apt-get install -y mongodb-org sudo systemctl start mongod sudo systemctl enable mongod

By default the server (the mongod process) listens on port 27017 and stores data in a data directory it manages for you.

Option 2: MongoDB Atlas in the Cloud

MongoDB Atlas is the official database-as-a-service. The free M0 tier gives you a 512 MB cluster that is perfect for learning, with nothing to install and automatic security defaults. To set it up:

  1. Create an account at the MongoDB Atlas website.
  2. Create a new project and click Build a Database, choosing the free M0 shared cluster.
  3. Pick a cloud provider and a region close to you.
  4. Create a database user with a username and password.
  5. Under Network Access, allow your current IP address.
  6. Click Connect to copy your connection string.

A typical Atlas connection string looks like this:

text
mongodb+srv://appuser:<password>@cluster0.abc12.mongodb.net/shop

Keep this string secret, because anyone who has it can reach your database. In real projects it belongs in an environment variable, never in source code.

Connecting with mongosh

mongosh is the interactive MongoDB shell. It ships with recent server installers and can also be installed on its own. Connecting is one command:

bash
# Local server mongosh # Atlas cluster mongosh "mongodb+srv://cluster0.abc12.mongodb.net/shop" --username appuser

Once connected, try a few commands to prove everything works:

javascript
db.runCommand({ ping: 1 }) // { ok: 1 } show dbs // list databases db.hello().me // which server you are talking to

mongosh is a full JavaScript environment, so you can declare variables, write loops, and call every database command from one prompt.

Meet MongoDB Compass

If you prefer a graphical interface, MongoDB Compass lets you browse collections, run queries visually, analyze schemas, and inspect index usage. Compass is excellent for exploring unfamiliar data, but learning the shell first will make you faster and will translate directly into driver code later in the series.

Local vs Atlas: Which Should You Use?

For this course either works. A local install keeps everything offline and under your control, which is handy on flights or restricted networks. Atlas mirrors how most teams run MongoDB in production, gives you backups and monitoring out of the box, and lets you practice from any machine. Many developers use both: Atlas for shared environments and a local server for quick experiments.

Key Takeaways

  • MongoDB Community Server installs on Windows, macOS, and Linux and listens on port 27017.
  • MongoDB Atlas offers a free M0 cluster with security and backups managed for you.
  • Always create a database user and restrict network access before connecting to Atlas.
  • mongosh is the modern shell and a full JavaScript REPL.
  • Connection strings are credentials; store them in environment variables.

Next up, we will create our first database and collections, and learn exactly how MongoDB organizes documents.

Installing MongoDB and Using Atlas - MongoDB | CodeYourCraft | CodeYourCraft