Installing MongoDB
MongoDB runs on Windows, macOS, and Linux. Choose your platform and follow the steps below. After installation, you'll have a running MongoDB server and the mongosh CLI tool to interact with it.
Ubuntu / Debian
macOS
Windows
Tip: MongoDB runs as a background service. On Linux/Mac, it starts automatically. On Windows, ensure the MongoDB service is running via Services.msc.
Your First Commands
Once MongoDB is installed and running, open mongosh (MongoDB's CLI) and try these basic commands:
List all databases
config 0.000GB
local 0.000GB
Use (switch to) a database
See what collections exist in the current database
Get the name of the current database
Create a collection explicitly
Key Difference from SQL: In MongoDB, you don't need to pre-create databases or collections. They're created automatically when you insert your first document. But it's good practice to create them explicitly for clarity.
Inserting Your First Document
A document is MongoDB's version of a row. It's a JSON-like object with key-value pairs. Let's insert a product into the FreshBasket database:
Insert a single document with nested data
What happened: MongoDB automatically assigned an _id field (an ObjectId) to uniquely identify this document. The supplier is a nested object (subdocument) โ MongoDB loves flexible, nested data.
Find all documents in the collection
Pretty print the results
Find a specific document
Count documents in a collection
Building the FreshBasket Schema
Let's create the core collections for an e-commerce database: customers, products, and orders. MongoDB doesn't enforce rigid schemas like SQL, so you define structure through your application code.
Create a customers collection with nested preferences
Create an orders collection with items array
MongoDB Advantage: In MongoDB, you can embed related data (like items inside orders) rather than join separate tables. This denormalization is fast and natural for document databases.
Verify all collections exist
orders
products
MongoDB Compass: The GUI Tool
MongoDB Compass is a visual GUI for exploring and managing MongoDB data. It's free and much easier to use than the CLI for browsing collections.
Download and Install Compass
Download from https://www.mongodb.com/products/compass and install for your platform.
Connect to Your Local Database
- Open MongoDB Compass.
- Connection string defaults to:
mongodb://localhost:27017 - Click "Connect" and you'll see a list of databases on the left side.
What You'll See
- Left sidebar: List of all databases and their collections.
- Main panel: Documents in the selected collection, displayed as pretty-printed JSON.
- Tabs: Collections, Indexes, Validation, and Aggregation Pipeline tabs for advanced features.
- Query builder: Click "Filter" to write MongoDB queries without typing.
Best Practice: Use mongosh for scripting and automation, but use Compass for exploring data, checking schema structure, and testing queries interactively.
Challenge
Insert a new product document into the FreshBasket database with this structure:
- name: "Fresh Tomatoes (2kg)"
- category: "Vegetables"
- price: 300
- currency: "KES"
- stock: 85
- supplier (nested object) with name and location
Then run a query to find it by name. Finally, count total documents in the products collection.
Use insertOne() with nested supplier object, then findOne({ name: "..." }) and countDocuments():
Test Your Knowledge
MongoDB is running. Documents await.
Join the Best AI Program: The AI-Era Data Engineer โ 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Data Engineer โ