MongoDB Step-by-Step Tutorial for Beginners

MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications.

MongoDB Step-by-Step Tutorial for Beginners

MongoDB Step-by-Step Tutorial for Beginners

MongoDB Step-by-Step Tutorial for Beginners

MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications. This tutorial will guide you through the basics, from installation to common operations. We'll assume you're using a local setup for learning purposes. For production, consider MongoDB Atlas (cloud-hosted service).

Prerequisites: Basic command-line knowledge and a supported OS (Windows, macOS, or Linux).

Step 1: Installation

MongoDB Community Edition is free and available for major operating systems. Download from the official website: https://www.mongodb.com/try/download/community.

On Windows:

  1. Download the MSI installer for your Windows version.
  2. Run the installer and follow the prompts. Select "Complete" setup and install as a service (recommended for easy startup).
  3. Add the MongoDB bin directory (e.g., C:\Program Files\MongoDB\Server\7.0\bin) to your system's PATH environment variable.
  4. Verify installation by opening Command Prompt and running mongod --version.

On macOS:

  1. Use Homebrew (install if not present: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)").
  2. Run brew tap mongodb/brew to add the MongoDB tap.
  3. Install with brew install mongodb-community@7.0.
  4. Start the service: brew services start mongodb/brew/mongodb-community.
  5. Verify: mongod --version.

On Ubuntu (Linux example):

  1. Import the public key: wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -.
  2. Create a list file: echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list.
  3. Update packages: sudo apt-get update.
  4. Install: sudo apt-get install -y mongodb-org.
  5. Start the service: sudo systemctl start mongod.
  6. Verify: mongod --version and check status with sudo systemctl status mongod.

Note: Version 7.0 is current as of 2025; check for updates. If issues arise, refer to official docs.

Step 2: Starting the MongoDB Server

  • Open a terminal/command prompt.
  • Run mongod (or if installed as service, it's already running).
  • The server listens on port 27017 by default. You'll see logs indicating it's ready.

Step 3: Connecting to MongoDB (Using Mongo Shell)

  • Open another terminal.
  • Run mongosh (the modern shell; older versions use mongo).
  • You'll enter the MongoDB shell prompt: test> (default database is 'test').
  • To exit: exit.

Step 4: Creating a Database

Databases are created implicitly when you use them.
1. In the shell, switch to a new database: use myDatabase.
2. MongoDB creates it when you insert data.
- Show databases: show dbs.
- Current database: db.

Step 5: Creating Collections and Inserting Documents (Create in CRUD)

Collections are like tables and are also created implicitly.
1. Insert a document: db.myCollection.insertOne({ name: "John Doe", age: 30, city: "New York" }).
- This creates 'myCollection' if it doesn't exist.
2. Insert multiple: db.myCollection.insertMany([{ name: "Jane Smith", age: 25 }, { name: "Bob Johnson", age: 35 }]).
- Documents are BSON objects with key-value pairs.

Step 6: Querying Data (Read in CRUD)

  1. Find all documents: db.myCollection.find().
  2. Pretty print: db.myCollection.find().pretty().
  3. Filter: db.myCollection.find({ age: { $gt: 25 } }) (age greater than 25).
  4. Limit results: db.myCollection.find().limit(2).
  5. Sort: db.myCollection.find().sort({ age: 1 }) (ascending by age).
  6. Use operators like $eq, $ne, $gt, $lt, $in, etc.

Step 7: Updating Data (Update in CRUD)

  1. Update one: db.myCollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } }).
  2. Update many: db.myCollection.updateMany({ age: { $lt: 30 } }, { $inc: { age: 1 } }) (increment age by 1).
  3. Replace: db.myCollection.replaceOne({ name: "John Doe" }, { name: "John Doe", age: 32, city: "Boston" }).

Step 8: Deleting Data (Delete in CRUD)

  1. Delete one: db.myCollection.deleteOne({ name: "Bob Johnson" }).
  2. Delete many: db.myCollection.deleteMany({ age: { $gt: 30 } }).
  3. Drop collection: db.myCollection.drop().
  4. Drop database: db.dropDatabase().

Step 9: Indexing

Indexes speed up queries.
1. Create index: db.myCollection.createIndex({ age: 1 }) (ascending on age).
2. Unique index: db.myCollection.createIndex({ name: 1 }, { unique: true }).
3. View indexes: db.myCollection.getIndexes().

Step 10: Aggregation

For complex data processing.
1. Basic pipeline: db.myCollection.aggregate([{ $match: { age: { $gt: 25 } } }, { $group: { _id: "$city", count: { $sum: 1 } } }]).
- Matches documents, groups by city, counts them.

Additional Topics

  • Tools: Use MongoDB Compass (GUI) for visual management.
  • Drivers: For app integration, use official drivers (e.g., Node.js: npm install mongodb).
  • Security: Enable authentication in production; create users with db.createUser().
  • Replication/Sharding: For high availability and scaling, set up replica sets or shards.

Practice these commands in your shell. For cloud setup, try MongoDB Atlas free tier. If you're using a programming language, refer to language-specific docs (e.g., PyMongo for Python).

This covers the fundamentals. For advanced topics, explore the official documentation or courses.

Last updated: Nov 12, 2025

MongoDB Step-by-Step Tutorial for Beginners

MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications.

MongoDB Step-by-Step Tutorial for Beginners

MongoDB Step-by-Step Tutorial for Beginners

MongoDB Step-by-Step Tutorial for Beginners

MongoDB is an open-source, document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON. It's designed for scalability, high performance, and handling unstructured or semi-structured data, making it popular for modern web and mobile applications. This tutorial will guide you through the basics, from installation to common operations. We'll assume you're using a local setup for learning purposes. For production, consider MongoDB Atlas (cloud-hosted service).

Prerequisites: Basic command-line knowledge and a supported OS (Windows, macOS, or Linux).

Step 1: Installation

MongoDB Community Edition is free and available for major operating systems. Download from the official website: https://www.mongodb.com/try/download/community.

On Windows:

  1. Download the MSI installer for your Windows version.
  2. Run the installer and follow the prompts. Select "Complete" setup and install as a service (recommended for easy startup).
  3. Add the MongoDB bin directory (e.g., C:\Program Files\MongoDB\Server\7.0\bin) to your system's PATH environment variable.
  4. Verify installation by opening Command Prompt and running mongod --version.

On macOS:

  1. Use Homebrew (install if not present: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)").
  2. Run brew tap mongodb/brew to add the MongoDB tap.
  3. Install with brew install mongodb-community@7.0.
  4. Start the service: brew services start mongodb/brew/mongodb-community.
  5. Verify: mongod --version.

On Ubuntu (Linux example):

  1. Import the public key: wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -.
  2. Create a list file: echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list.
  3. Update packages: sudo apt-get update.
  4. Install: sudo apt-get install -y mongodb-org.
  5. Start the service: sudo systemctl start mongod.
  6. Verify: mongod --version and check status with sudo systemctl status mongod.

Note: Version 7.0 is current as of 2025; check for updates. If issues arise, refer to official docs.

Step 2: Starting the MongoDB Server

  • Open a terminal/command prompt.
  • Run mongod (or if installed as service, it's already running).
  • The server listens on port 27017 by default. You'll see logs indicating it's ready.

Step 3: Connecting to MongoDB (Using Mongo Shell)

  • Open another terminal.
  • Run mongosh (the modern shell; older versions use mongo).
  • You'll enter the MongoDB shell prompt: test> (default database is 'test').
  • To exit: exit.

Step 4: Creating a Database

Databases are created implicitly when you use them.
1. In the shell, switch to a new database: use myDatabase.
2. MongoDB creates it when you insert data.
- Show databases: show dbs.
- Current database: db.

Step 5: Creating Collections and Inserting Documents (Create in CRUD)

Collections are like tables and are also created implicitly.
1. Insert a document: db.myCollection.insertOne({ name: "John Doe", age: 30, city: "New York" }).
- This creates 'myCollection' if it doesn't exist.
2. Insert multiple: db.myCollection.insertMany([{ name: "Jane Smith", age: 25 }, { name: "Bob Johnson", age: 35 }]).
- Documents are BSON objects with key-value pairs.

Step 6: Querying Data (Read in CRUD)

  1. Find all documents: db.myCollection.find().
  2. Pretty print: db.myCollection.find().pretty().
  3. Filter: db.myCollection.find({ age: { $gt: 25 } }) (age greater than 25).
  4. Limit results: db.myCollection.find().limit(2).
  5. Sort: db.myCollection.find().sort({ age: 1 }) (ascending by age).
  6. Use operators like $eq, $ne, $gt, $lt, $in, etc.

Step 7: Updating Data (Update in CRUD)

  1. Update one: db.myCollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } }).
  2. Update many: db.myCollection.updateMany({ age: { $lt: 30 } }, { $inc: { age: 1 } }) (increment age by 1).
  3. Replace: db.myCollection.replaceOne({ name: "John Doe" }, { name: "John Doe", age: 32, city: "Boston" }).

Step 8: Deleting Data (Delete in CRUD)

  1. Delete one: db.myCollection.deleteOne({ name: "Bob Johnson" }).
  2. Delete many: db.myCollection.deleteMany({ age: { $gt: 30 } }).
  3. Drop collection: db.myCollection.drop().
  4. Drop database: db.dropDatabase().

Step 9: Indexing

Indexes speed up queries.
1. Create index: db.myCollection.createIndex({ age: 1 }) (ascending on age).
2. Unique index: db.myCollection.createIndex({ name: 1 }, { unique: true }).
3. View indexes: db.myCollection.getIndexes().

Step 10: Aggregation

For complex data processing.
1. Basic pipeline: db.myCollection.aggregate([{ $match: { age: { $gt: 25 } } }, { $group: { _id: "$city", count: { $sum: 1 } } }]).
- Matches documents, groups by city, counts them.

Additional Topics

  • Tools: Use MongoDB Compass (GUI) for visual management.
  • Drivers: For app integration, use official drivers (e.g., Node.js: npm install mongodb).
  • Security: Enable authentication in production; create users with db.createUser().
  • Replication/Sharding: For high availability and scaling, set up replica sets or shards.

Practice these commands in your shell. For cloud setup, try MongoDB Atlas free tier. If you're using a programming language, refer to language-specific docs (e.g., PyMongo for Python).

This covers the fundamentals. For advanced topics, explore the official documentation or courses.

Last updated: Nov 12, 2025