MongoDB in One Shot - The Ultimate Cheatsheet

    MongoDB in One Shot - The Ultimate Cheatsheet

    A concise MongoDB cheatsheet designed to help you quickly understand and revise core concepts. It covers everything from fundamentals and CRUD operations to advanced querying, indexing, aggregation, and schema design, with practical examples for real-world applications. Ideal for learning, quick revision, and interview preparation.

    default profile

    Shreyash Gurav

    March 17, 2026

    14 min read

    MongoDB in One Shot - The Ultimate Cheatsheet

    MongoDB is a powerful NoSQL database built for modern applications that demand flexibility, speed, and scalability. Instead of rigid tables and schemas, it stores data in JSON-like documents, making it easy to model real-world data and iterate quickly as requirements evolve.

    Designed for high performance and distributed systems, MongoDB enables developers to handle large-scale data with ease through features like indexing, aggregation, replication, and sharding. It is widely used in building scalable backends, real-time applications, and microservices architectures.


    1. MongoDB Fundamentals#

    What is MongoDB?#

    MongoDB is a NoSQL, document-oriented database that stores data as flexible, JSON-like documents instead of rows and columns. It is schema-less by default, meaning each document in a collection can have different fields.

    Why MongoDB over SQL?

    • Stores hierarchical/nested data naturally
    • Horizontally scalable
    • Great for unstructured or semi-structured data
    • Fast reads on large datasets with proper indexing

    JSON vs BSON#

    FeatureJSONBSON
    FormatText-basedBinary-encoded
    Data TypesLimitedExtended (Date, ObjectId, etc.)
    SpeedSlower parseFaster for MongoDB internals
    Used ForHuman-readableMongoDB storage + wire protocol

    MongoDB stores data as BSON internally but exposes it as JSON to the developer.

    Database → Collection → Document Hierarchy#

    MongoDB Instance └── Database (e.g., "shopDB") └── Collection (e.g., "products") └── Document (e.g., { name: "Laptop", price: 999 })
    • Database - container for collections
    • Collection - equivalent to a table in SQL
    • Document - equivalent to a row; a BSON object
    MongoDB Document Structure

    The _id Field#

    Every document must have a unique _id field. If you don't provide one, MongoDB auto-generates an ObjectId.

    { _id: ObjectId("64a3f8c2d1e2a3b4c5d6e7f8"), name: "Alice", age: 28 }
    • ObjectId is 12 bytes: 4-byte timestamp + 5-byte random + 3-byte incrementing counter
    • You can use any unique value as _id (string, integer, etc.)

    Installation Checklist#

    • MongoDB Community Server - the core database engine
    • MongoDB Compass - GUI for exploring and querying data visually
    • mongosh - the official MongoDB shell for terminal-based interaction
    # Start mongosh mongosh # Select or create a database use shopDB # Check current db db # List all databases show dbs # List collections show collections

    2. Core CRUD Operations#

    MongoDB CRUD Operations

    Insert#

    // Insert a single document db.users.insertOne({ name: "Ravi", email: "ravi@example.com", age: 25, city: "Mumbai" }) // Insert multiple documents db.users.insertMany([ { name: "Priya", age: 22, city: "Delhi" }, { name: "Arjun", age: 30, city: "Bangalore" } ])
    • insertOne() returns insertedId
    • insertMany() returns an array of insertedIds

    Read#

    // Find all documents db.users.find() // Find with a filter db.users.find({ city: "Mumbai" }) // Find one document db.users.findOne({ name: "Ravi" }) // Pretty print in older shell db.users.find().pretty()

    Update#

    // Update a single document db.users.updateOne( { name: "Ravi" }, { $set: { age: 26 } } ) // Update multiple documents db.users.updateMany( { city: "Mumbai" }, { $set: { country: "India" } } ) // Increment a value db.users.updateOne( { name: "Ravi" }, { $inc: { age: 1 } } ) // Push to an array db.users.updateOne( { name: "Ravi" }, { $push: { hobbies: "photography" } } ) // Pull from an array db.users.updateOne( { name: "Ravi" }, { $pull: { hobbies: "photography" } } )

    Delete#

    // Delete one db.users.deleteOne({ name: "Ravi" }) // Delete many db.users.deleteMany({ city: "Delhi" }) // Delete all documents in a collection db.users.deleteMany({})

    Common Update Operators#

    OperatorPurpose
    $setSet or update a field value
    $unsetRemove a field
    $incIncrement a numeric field
    $pushAdd an element to an array
    $pullRemove matching elements from array
    $addToSetAdd to array only if not already present
    $renameRename a field

    3. Advanced Querying Techniques#

    MongoDB provides a flexible query language to filter, combine conditions, and shape results. Queries operate on documents and can leverage indexes for efficient execution.


    Comparison Operators#

    Comparison operators filter documents based on field values, similar to SQL WHERE conditions.

    // Greater than db.products.find({ price: { $gt: 500 } }) // Less than or equal db.products.find({ price: { $lte: 1000 } }) // Equal (explicit) db.products.find({ status: { $eq: "active" } }) // Not equal db.products.find({ status: { $ne: "inactive" } }) // In a list db.products.find({ category: { $in: ["Electronics", "Books"] } }) // Not in a list db.products.find({ category: { $nin: ["Clothing"] } })

    These operators are evaluated per document. MongoDB can efficiently use indexes with operators like $eq, $gt, and $lt. The $in operator is useful for matching multiple values but can become slower with very large arrays.

    OperatorMeaning
    $gtGreater than
    $gteGreater than or equal
    $ltLess than
    $lteLess than or equal
    $eqEqual
    $neNot equal
    $inMatches any value in array
    $ninMatches none of the values

    Logical Operators#

    Logical operators combine multiple conditions to form complex queries.

    // $and - both conditions must match db.users.find({ $and: [{ age: { $gte: 18 } }, { city: "Mumbai" }] }) // $or - either condition matches db.users.find({ $or: [{ city: "Delhi" }, { city: "Mumbai" }] }) // $not - negates a condition db.users.find({ age: { $not: { $gt: 30 } } }) // $nor - none of the conditions match db.users.find({ $nor: [{ city: "Delhi" }, { age: { $lt: 18 } }] })

    MongoDB implicitly applies $and when multiple conditions are specified in a single query object. $or queries may require proper indexing for good performance. $not and $nor are generally less efficient and should be used carefully on large datasets.


    Projection#

    Projection controls which fields are returned in the result set.

    // Return only name and city, exclude _id db.users.find({}, { name: 1, city: 1, _id: 0 }) // Exclude a field db.users.find({}, { password: 0 })

    Projection reduces the amount of data transferred from the database and can improve performance. It also enables covered queries when all required fields are present in an index. Inclusion and exclusion cannot be mixed in the same projection, except for _id.


    Sorting#

    Sorting arranges documents based on specified fields.

    // Ascending (1), Descending (-1) db.products.find().sort({ price: 1 }) // cheapest first db.products.find().sort({ price: -1 }) // most expensive first db.products.find().sort({ name: 1, price: -1 }) // multi-sort

    Sorting without an index may result in in-memory operations, which are slower. For better performance, indexes should be created on fields used in sorting. Compound indexes should match the sort order.


    Limiting and Skipping#

    Used to control the number of documents returned and implement pagination.

    // Return only 5 documents db.products.find().limit(5) // Skip first 10, then get 5 (pagination) db.products.find().skip(10).limit(5)

    The limit() function reduces the number of documents returned, improving efficiency. The skip() function can become inefficient on large datasets because MongoDB still scans skipped documents internally.

    For scalable pagination, cursor-based approaches (e.g., using _id or indexed fields) are preferred over large skip() values.


    4. Indexing and Query Optimization#

    Without an index, MongoDB performs a collection scan - checking every document. With an index, it jumps directly to matching documents. Think of it like a book index versus reading every page.

    MongoDB Indexing

    Creating Indexes#

    // Single field index db.users.createIndex({ email: 1 }) // Compound index (multiple fields) db.orders.createIndex({ userId: 1, createdAt: -1 }) // Unique index db.users.createIndex({ email: 1 }, { unique: true }) // Text index (full-text search) db.articles.createIndex({ content: "text" }) // List all indexes on a collection db.users.getIndexes() // Drop an index db.users.dropIndex("email_1")

    Text Search with Text Index#

    db.articles.find({ $text: { $search: "mongodb tutorial" } })

    Explain - Understand Query Performance#

    db.users.find({ email: "ravi@example.com" }).explain("executionStats")

    Key fields to check in explain output:

    • COLLSCAN - no index used, bad for large collections
    • IXSCAN - index used, efficient
    • nReturned - documents returned
    • totalDocsExamined - documents scanned

    Indexing Best Practices#

    • Index fields used in find(), sort(), and $lookup
    • Compound indexes follow the ESR rule: Equality first, Sort next, Range last
    • Too many indexes slow down writes
    • Use explain() to verify your index is being used
    • TTL indexes can auto-expire documents (useful for sessions, logs)
    // TTL index - auto delete after 3600 seconds db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })

    5. Schema Design and Data Modeling#

    MongoDB is schema-flexible, but that doesn't mean you should design carelessly. The right schema design depends on your read/write patterns.

    Embedding vs Referencing#

    Embedding - store related data inside the same document

    // Embedded address inside user { _id: ObjectId("..."), name: "Priya", address: { street: "123 MG Road", city: "Pune", pincode: "411001" } }

    Referencing - store the related document's _id as a foreign key

    // User document { _id: ObjectId("u1"), name: "Priya" } // Order document references user { _id: ObjectId("o1"), userId: ObjectId("u1"), total: 4500 }

    When to Embed vs Reference#

    ScenarioRecommendation
    Data always read togetherEmbed
    Child data is small and boundedEmbed
    Data grows unboundedly (e.g., comments)Reference
    Data is shared across many documentsReference
    Need to query child data independentlyReference

    Denormalization#

    MongoDB often duplicates data intentionally (denormalization) to avoid expensive joins at read time. For example, storing a user's name alongside each order even though it also exists in the users collection - this way you never need a join to display order history.

    Schema Design Principles#

    • Design your schema around how your application queries data, not how the data naturally relates
    • Avoid unbounded arrays inside documents (keep arrays small and finite)
    • A document size limit of 16 MB applies in MongoDB
    • Use ObjectId references when relationships are complex or data is large

    6. Aggregation Framework Deep Dive#

    The aggregation pipeline processes documents through a series of stages, where each stage transforms the data. Think of it as a conveyor belt - documents go in, get filtered, grouped, reshaped, and come out the other end.

    MongoDB Aggregation Pipeline

    Basic Pipeline Syntax#

    db.collection.aggregate([ { stage1 }, { stage2 }, { stage3 } ])

    $match - Filter Documents#

    Works like find(). Always put $match first to reduce documents early.

    db.orders.aggregate([ { $match: { status: "delivered" } } ])

    $group - Group and Aggregate#

    // Total revenue per city db.orders.aggregate([ { $group: { _id: "$city", totalRevenue: { $sum: "$amount" }, orderCount: { $sum: 1 }, avgOrder: { $avg: "$amount" } } } ])

    Common $group accumulators:

    AccumulatorPurpose
    $sumTotal sum
    $avgAverage value
    $minMinimum value
    $maxMaximum value
    $countCount of documents
    $pushCollect values into array
    $firstFirst value in group
    $lastLast value in group

    $project - Reshape Documents#

    db.users.aggregate([ { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] }, age: 1, _id: 0 } } ])

    $sort and $limit#

    db.orders.aggregate([ { $match: { status: "delivered" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } } }, { $sort: { total: -1 } }, { $limit: 10 } ])

    This pipeline finds the top 10 customers by total spending.

    $unwind - Flatten Arrays#

    // Each element of the array becomes its own document db.orders.aggregate([ { $unwind: "$items" } ])

    $lookup - Join Collections#

    db.orders.aggregate([ { $lookup: { from: "users", // collection to join localField: "userId", // field in orders foreignField: "_id", // field in users as: "userDetails" // output array field } } ])

    7. Relationships and Joins in MongoDB#

    MongoDB is not a relational database, but you can model and query relationships effectively.

    One-to-One#

    // Embedded (preferred for one-to-one) { _id: ObjectId("u1"), name: "Arjun", profile: { bio: "Developer", website: "arjun.dev" } }

    One-to-Many#

    // Author has many books - reference approach { _id: ObjectId("a1"), name: "Chetan Bhagat" } { _id: ObjectId("b1"), title: "2 States", authorId: ObjectId("a1") } { _id: ObjectId("b2"), title: "3 Mistakes", authorId: ObjectId("a1") } // Query using $lookup db.books.aggregate([ { $lookup: { from: "authors", localField: "authorId", foreignField: "_id", as: "author" } } ])

    Many-to-Many#

    Use an intermediary collection (like a junction table in SQL) or embed an array of references.

    // Students and courses - array of references { _id: ObjectId("s1"), name: "Neha", enrolledCourses: [ObjectId("c1"), ObjectId("c2")] } { _id: ObjectId("c1"), title: "MongoDB Basics" } // Or a separate enrollment collection { studentId: ObjectId("s1"), courseId: ObjectId("c1"), enrolledAt: ISODate("2024-01-10") }

    8. Transactions and Data Consistency#

    By default, individual document operations in MongoDB are atomic. If you need to update multiple documents atomically, you need multi-document transactions (available from MongoDB 4.0+ with replica sets).

    ACID in MongoDB#

    PropertyMongoDB Support
    AtomicitySingle document always; multi-doc via transactions
    ConsistencyEnforced via schema validation + transactions
    IsolationSnapshot isolation within transactions
    DurabilityWrite concerns + journaling

    Multi-Document Transactions#

    const session = client.startSession() try { session.startTransaction() await db.collection("accounts").updateOne( { _id: "acc1" }, { $inc: { balance: -500 } }, { session } ) await db.collection("accounts").updateOne( { _id: "acc2" }, { $inc: { balance: 500 } }, { session } ) await session.commitTransaction() } catch (err) { await session.abortTransaction() } finally { session.endSession() }

    Key Points About Transactions#

    • Transactions require a replica set or sharded cluster - they don't work on standalone instances
    • Keep transactions short and fast to avoid lock contention
    • Transactions have a default timeout of 60 seconds
    • Use transactions only when you truly need multi-document atomicity; single-document operations are cheaper

    9. MongoDB Atlas (Cloud Database)#

    MongoDB Atlas is the fully managed cloud version of MongoDB. It handles provisioning, backups, scaling, and security for you.

    MongoDB Atlas Dashboard Overview

    Setting Up a Free Cluster#

    1. Go to cloud.mongodb.com and sign up
    2. Create a new project
    3. Build a cluster - choose M0 (free tier) for learning
    4. Create a database user with username and password
    5. Whitelist your IP address under Network Access
    6. Click Connect to get your connection string

    Connection String Format#

    mongodb+srv://<username>:<password>@cluster0.xxxxx.mongodb.net/<dbname>?retryWrites=true&w=majority

    Atlas Features Worth Knowing#

    • Atlas Search - full-text search powered by Lucene
    • Atlas Triggers - run serverless functions on database events
    • Atlas Data API - access data over HTTP without a driver
    • Backups - automated point-in-time backups
    • Performance Advisor - suggests indexes based on slow queries

    10. Security and Access Control#

    Authentication#

    MongoDB supports several authentication mechanisms:

    • SCRAM (default) - username/password
    • X.509 - certificate-based
    • LDAP - enterprise only
    • Kerberos - enterprise only
    # Connect with authentication mongosh "mongodb://username:password@localhost:27017/dbname"

    Built-in Roles#

    RoleAccess Level
    readRead all collections in a DB
    readWriteRead and write to a DB
    dbAdminAdmin tasks, no user management
    userAdminManage users and roles
    clusterAdminManage the entire cluster
    readAnyDatabaseRead across all databases
    rootSuperuser - full access

    Creating a User#

    use admin db.createUser({ user: "appUser", pwd: "securePassword123", roles: [ { role: "readWrite", db: "shopDB" } ] })

    Data Protection Best Practices#

    • Always enable authentication (disabled by default in local dev)
    • Use TLS/SSL for connections in production
    • Enable encryption at rest (Atlas does this automatically)
    • Never expose MongoDB port (27017) directly to the internet
    • Rotate credentials regularly
    • Use schema validation to prevent malformed data
    // Adding schema validation to a collection db.createCollection("users", { validator: { $jsonSchema: { bsonType: "object", required: ["name", "email"], properties: { email: { bsonType: "string" } } } } })

    11. Scaling and Performance Optimization#

    MongoDB Replication and Sharding Architecture

    Replication#

    A replica set is a group of MongoDB instances that hold the same data. It provides high availability and data redundancy.

    Primary Node──writes──> Secondary Node 1 └────> Secondary Node 2
    • Reads go to the primary by default; can be configured to read from secondaries
    • If the primary goes down, an automatic election picks a new primary
    • Minimum recommended: 3 nodes (1 primary + 2 secondaries)
    • Write concern and read preference control consistency vs performance tradeoffs
    // Connect to replica set mongosh "mongodb://host1:27017,host2:27017,host3:27017/dbname?replicaSet=myReplicaSet"

    Sharding#

    Sharding distributes data horizontally across multiple machines. Use it when a single machine can no longer handle the data volume or throughput.

    • Shard - a replica set holding a subset of data
    • Mongos - the router that directs queries to the right shard
    • Config Servers - store cluster metadata
    • Shard Key - the field used to distribute data across shards
    // Enable sharding on a database sh.enableSharding("shopDB") // Shard a collection by a key sh.shardCollection("shopDB.orders", { customerId: "hashed" })

    Choosing a good shard key:

    • High cardinality (many unique values)
    • Even distribution of reads and writes
    • Avoid monotonically increasing fields like timestamps as the sole shard key (causes hotspots)

    Query Optimization Checklist#

    • Always use explain("executionStats") to analyze slow queries
    • Ensure commonly queried fields are indexed
    • Avoid using $where (executes JavaScript - very slow)
    • Use projections to return only needed fields
    • Avoid large skip() values on big collections; use cursor-based pagination instead
    • Keep documents lean - don't store fields you never query
    • Use covered queries (index covers all fields in query + projection - no document fetch needed)
    // Covered query example // Index: { email: 1, name: 1 } db.users.find( { email: "test@example.com" }, { name: 1, _id: 0 } ) // MongoDB can return the result purely from the index, without touching documents

    Quick Reference Card#

    Most Used Commands#

    // Database use dbName show dbs show collections db.dropDatabase() // CRUD db.col.insertOne({}) db.col.insertMany([]) db.col.find({ field: value }) db.col.findOne({ field: value }) db.col.updateOne({ filter }, { $set: {} }) db.col.updateMany({ filter }, { $set: {} }) db.col.deleteOne({ filter }) db.col.deleteMany({ filter }) // Aggregation db.col.aggregate([ {$match:{}}, {$group:{}}, {$sort:{}} ]) // Indexes db.col.createIndex({ field: 1 }) db.col.getIndexes() db.col.dropIndex("indexName") // Explain db.col.find({}).explain("executionStats")

    Operator Quick Reference#

    CategoryOperators
    Comparison$eq $ne $gt $gte $lt $lte $in $nin
    Logical$and $or $not $nor
    Update$set $unset $inc $push $pull $addToSet $rename
    Array$elemMatch $size $all
    Aggregation$match $group $project $sort $limit $skip $lookup $unwind
    Accumulators$sum $avg $min $max $first $last $push $count

    Conclusion#

    MongoDB offers a flexible and powerful approach to data modeling, making it an excellent choice for modern, scalable applications. With its document-based structure, rich query capabilities, and support for indexing, aggregation, and distributed systems, it enables developers to build high-performance backends with ease.

    Mastering MongoDB is not just about learning queries, but understanding how to design schemas, optimize performance, and scale systems effectively. By applying the concepts in this guide, you can confidently use MongoDB in real-world applications and production environments.

    If you found this helpful, consider sharing it with your friends and peers who are learning or working with MongoDB.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more
    MangoDB
    NoSQL
    Database
    Backend
    CRUD
    Was it helpful?

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.

    More articles