DEV Community

Python Tutorial
Python Tutorial

Posted on

1

MongoDB Tutorial: Learn CRUD Operations, Indexing, and Aggregation

MongoDB Tutorial

Image description


Introduction

If you're diving into the world of NoSQL databases, MongoDB is one of the best technologies to explore. Designed for flexibility, scalability, and high performance, MongoDB allows you to store and query large volumes of data with ease. In this MongoDB tutorial, we’ll walk you through the core features you need to know to work with MongoDB efficiently — specifically CRUD operations, indexing, and aggregation. Whether you're a developer, data analyst, or tech enthusiast, this guide will help you get started with MongoDB the right way.

What is MongoDB?

MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents known as BSON. Unlike traditional relational databases, MongoDB doesn’t rely on fixed schemas, making it ideal for dynamic applications and rapid development.

Some of its standout features include:

  • Horizontal scalability
  • Built-in replication
  • Schema-less design
  • Rich query language

Now, let’s get hands-on and explore the basics.


Get Started with MongoDB

To follow along with this MongoDB tutorial, you’ll need to install MongoDB and set up your environment.

Installing MongoDB

  1. Visit the official Tpoint Tech website for installing MongoDB: https://www.tpointtech.com/how-to-install-mongodb-on-windows
  2. Choose your operating system and download the Community Server.
  3. Follow the installation instructions specific to your OS.
  4. Once installed, launch the MongoDB server with:
mongod
Enter fullscreen mode Exit fullscreen mode
  1. Open a new terminal and start the Mongo shell:
mongo
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use MongoDB Compass, the official GUI tool, for visual database interaction.


CRUD Operations in MongoDB

CRUD stands for Create, Read, Update, and Delete — the four basic operations used to interact with a database. Let's break down each one using MongoDB syntax.

1. Create (Insert)

You can insert a document into a collection using insertOne() or insertMany().

db.users.insertOne({ name: "Alice", age: 25, city: "New York" });
Enter fullscreen mode Exit fullscreen mode

2. Read (Find)

Use find() to retrieve documents.

db.users.find({ name: "Alice" });
Enter fullscreen mode Exit fullscreen mode

To retrieve all documents:

db.users.find({});
Enter fullscreen mode Exit fullscreen mode

3. Update

Use updateOne() or updateMany() to modify documents.

db.users.updateOne(
  { name: "Alice" },
  { $set: { age: 26 } }
);
Enter fullscreen mode Exit fullscreen mode

4. Delete

Use deleteOne() or deleteMany() to remove documents.

db.users.deleteOne({ name: "Alice" });
Enter fullscreen mode Exit fullscreen mode

These operations form the foundation of interacting with any MongoDB database.


Indexing in MongoDB

As your database grows, queries can become slower. Indexing helps optimize performance by allowing MongoDB to search documents faster.

Creating an Index

Let’s say you frequently query by the city field. You can create an index like this:

db.users.createIndex({ city: 1 });
Enter fullscreen mode Exit fullscreen mode

The 1 indicates ascending order. You can also create compound indexes using multiple fields:

db.users.createIndex({ city: 1, age: -1 });
Enter fullscreen mode Exit fullscreen mode

Viewing Indexes

To list all indexes in a collection:

db.users.getIndexes();
Enter fullscreen mode Exit fullscreen mode

Dropping an Index

To remove an index:

db.users.dropIndex({ city: 1 });
Enter fullscreen mode Exit fullscreen mode

Proper indexing can drastically improve performance, especially in read-heavy applications.


Aggregation in MongoDB

Aggregation operations process data records and return computed results. They’re often used for grouping, filtering, and transforming data — similar to SQL’s GROUP BY and JOIN.

Basic Aggregation Example

db.users.aggregate([
  { $match: { city: "New York" } },
  { $group: { _id: "$city", total: { $sum: 1 } } }
]);
Enter fullscreen mode Exit fullscreen mode

Common Aggregation Operators

  • $match: Filters documents
  • $group: Groups documents by a specified key
  • $sort: Sorts documents
  • $project: Reshapes documents by including/excluding fields
  • $limit: Limits the number of documents returned

Example: Average Age by City

db.users.aggregate([
  { $group: { _id: "$city", avgAge: { $avg: "$age" } } }
]);
Enter fullscreen mode Exit fullscreen mode

Aggregation pipelines are powerful tools for real-time analytics and reporting.


Final Thoughts

This MongoDB tutorial covered essential concepts that every beginner should know: CRUD operations, indexing, and aggregation. Once you get started with MongoDB, you’ll quickly realize how flexible and powerful it is, especially for handling semi-structured data and fast-changing application requirements.

With the rise of modern web applications, microservices, and big data platforms, MongoDB continues to grow in popularity as a go-to NoSQL solution. Mastering the basics will prepare you to build scalable, high-performance applications with ease.

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Sentry image

Make it make sense

Only get the information you need to fix your code that’s broken with Sentry.

Start debugging →