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

bash

macOS

bash

Windows

bash

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

javascript
admin 0.000GB
config 0.000GB
local 0.000GB

Use (switch to) a database

javascript
switched to db freshbasket

See what collections exist in the current database

javascript

Get the name of the current database

javascript
freshbasket

Create a collection explicitly

javascript

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

javascript
{ acknowledged: true, insertedId: ObjectId("69e0a6a14d75051788b18deb") }

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

javascript

Pretty print the results

javascript

Find a specific document

javascript

Count documents in a collection

javascript
1

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

javascript

Create an orders collection with items array

javascript

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

javascript
customers
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.

Show hint

Use insertOne() with nested supplier object, then findOne({ name: "..." }) and countDocuments():

javascript

Test Your Knowledge

Question 1 of 5
What does MongoDB's _id field contain?
Question 2 of 5
How do you create a new database in MongoDB?
Question 3 of 5
What's the difference between find() and findOne()?
Question 4 of 5
How do you insert multiple documents at once?
Question 5 of 5
Which two tools can you use to query MongoDB?
0
/5
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’