MongoDB CheatSheet with Example and Definition

MongoDB is a popular NoSQL database known for its flexibility and scalability. To help you navigate and reference key MongoDB concepts quickly, we have created a comprehensive MongoDB cheat sheet. This cheat sheet provides clear definitions and practical examples for various MongoDB operations and functionalities.

MongoDB Basics

Definition: MongoDB is a document-oriented NoSQL database that stores data in flexible JSON-like documents.


A MongoDB document representing a user:

  _id: ObjectId("61abcf12345"),
  name: "John Doe",
  age: 30,
  email: "",
  hobbies: ["reading", "coding", "hiking"]

CRUD Operations

Definition: CRUD operations refer to the basic database operations: Create, Read, Update, and Delete.


1. Create: Inserting a document into a collection:

db.users.insertOne({ name: "Alice", age: 25 });

2. Read: Querying documents from a collection:

db.users.find({ age: { $gt: 20 } });

3. Update: Modifying a document in a collection:

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

4. Delete: Removing a document from a collection:

db.users.deleteOne({ name: "Alice" });

Query Operators

Definition: MongoDB provides powerful query operators to perform advanced document queries.


1. Comparison Operators:

db.users.find({ age: { $gte: 25 } });

2. Logical Operators:

db.users.find({ $or: [{ age: { $lt: 18 } }, { age: { $gte: 65 } }] });

3. Array Operators:

db.users.find({ hobbies: { $in: ["reading", "coding"] } });

Aggregation Framework

Definition: The Aggregation Framework allows you to perform complex data analysis and transformations.


Aggregating documents based on a field:

  { $group: { _id: "$age", count: { $sum: 1 } } },
  { $sort: { count: -1 } }


Definition: Indexes enhance query performance by allowing faster data retrieval.


Creating an index on the “name” field:

db.users.createIndex({ name: 1 });

Data Modeling

Definition: Data modeling in MongoDB involves designing the structure and relationships of your data.


Embedding related documents within a parent document:

  _id: ObjectId("61abcde12345"),
  name: "John Doe",
  address: {
    street: "123 Main St",
    city: "New York",
    country: "USA"

MongoDB Atlas

Definition: MongoDB Atlas is a fully managed cloud database service for MongoDB.


Connecting to a MongoDB Atlas cluster:

const { MongoClient } = require("mongodb");
const uri = "mongodb+srv://<username>:<password>@<cluster-url>/test";
const client = new MongoClient(uri);


This MongoDB cheat sheet provides definitions and practical examples for various MongoDB concepts, including basic operations, query operators, the aggregation framework, indexing, data modeling, and MongoDB Atlas. Keep this cheat sheet handy to quickly reference MongoDB functionalities and enhance your MongoDB development skills.