Skip to content

MongoDB Document Store

Easy ~15 min

What you'll build

A set of MongoDB operations that create a blog collection with flexible documents, query by embedded fields, and update nested data.

Goals

  • Insert flexible documents with varying fields
  • Query by embedded and nested fields
  • Update nested document fields
  • Understand the differences from relational design

Prerequisites

  • Basic understanding of JSON
  • Database concepts
  • NoSQL overview

Steps

  1. 1

    Insert blog posts as documents

    Insert two blog post documents. Notice they have different fields — no fixed schema required.

    Code
    db.posts.insertOne({
      title: "Learning SQL",
      author: { name: "Ada", email: "[email protected]" },
      tags: ["sql", "database"],
      views: 142,
      published: true
    });
    
    db.posts.insertOne({
      title: "NoSQL Basics",
      author: { name: "Grace", role: "admin" },
      tags: ["nosql", "mongodb"],
      views: 89,
      published: true,
      featured: true  // this field doesn't exist in the first doc
    });

    Expected result

    Two documents are inserted with different field sets.

  2. 2

    Query by nested field

    Find all posts by a specific author by querying the nested author.name field.

    Code
    db.posts.find({ "author.name": "Ada" });

    Expected result

    Returns the post authored by Ada.

  3. 3

    Query by array element

    Find all posts that have a specific tag. MongoDB can query inside arrays directly.

    Code
    db.posts.find({ tags: "sql" });

    Expected result

    Returns all posts tagged with 'sql'.

  4. 4

    Update a nested field

    Increment the view count of a post and add a new tag using MongoDB update operators.

    Code
    db.posts.updateOne(
      { title: "Learning SQL" },
      {
        $inc: { views: 1 },
        $push: { tags: "tutorial" }
      }
    );

    Expected result

    The post's views increase by 1 and 'tutorial' is added to its tags.

  5. 5

    Add indexes to optimize queries

    Create indexes on frequently queried columns to improve query performance. Indexes are the primary means of database performance optimization, but be mindful of write overhead.

    Code
    -- Create indexes on frequently filtered columns
    CREATE INDEX idx_users_email ON users(email);
    CREATE INDEX idx_orders_user_id ON orders(user_id);
    CREATE INDEX idx_orders_created_at ON orders(created_at DESC);
    
    -- Verify the index is used
    EXPLAIN SELECT * FROM orders WHERE user_id = 42;

    Expected result

    EXPLAIN output shows an index scan using idx_orders_user_id instead of a full table scan.

Try it yourself

What you learned

You worked with MongoDB's flexible document model, querying nested fields and arrays, and using update operators — experiencing how NoSQL differs from relational databases.

Related Resources

Related Lessons

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic