Back to Modules

MongoDB Tutorial

Master MongoDB fundamentals with our comprehensive tutorial. Learn through examples and hands-on practice.

Video Tutorial

MongoDB Document Structure

MongoDB stores data in flexible, JSON-like documents. Let's understand the document structure and how data is organized.

Examples:

// MongoDB Document Structure
// A MongoDB document (similar to a row in SQL)
const userDocument = {
  _id: "507f1f77bcf86cd799439011",
  name: "John Doe",
  age: 30,
  email: "john@example.com",
  address: {
    street: "123 Main St",
    city: "New York",
    country: "USA"
  },
  hobbies: ["reading", "coding", "gaming"],
  createdAt: new Date("2024-01-15")
};

// Document Features:
// • _id: Unique identifier (auto-generated)
// • Nested objects: address field contains sub-document
// • Arrays: hobbies field stores multiple values
// • Flexible schema: No predefined structure needed

Understanding MongoDB document structure with nested objects and arrays

CRUD Operations in MongoDB

Learn how to perform Create, Read, Update, and Delete operations in MongoDB. This simulation shows what happens when you execute these operations.

Examples:

// MongoDB CRUD Operations
import { MongoClient } from 'mongodb';

async function crudOperations() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('myDatabase');
  const collection = db.collection('users');

  // CREATE - insertOne()
  const newUser = {
    name: "John Doe",
    age: 30,
    email: "john@example.com"
  };
  await collection.insertOne(newUser);

  // READ - findOne()
  const foundUser = await collection.findOne({ name: "John Doe" });

  // UPDATE - updateOne()
  await collection.updateOne(
    { name: "John Doe" },
    { $set: { age: 31 } }
  );

  // DELETE - deleteOne()
  await collection.deleteOne({ name: "John Doe" });

  await client.close();
}

crudOperations();

Simulating MongoDB CRUD operations with actual results

MongoDB Query Operators

MongoDB provides powerful query operators to filter and find documents. Learn the most common operators used in queries.

Examples:

// MongoDB Query Operators
import { MongoClient } from 'mongodb';

async function queryExamples() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const collection = client.db('shop').collection('products');

  // $gt (greater than)
  const expensive = await collection.find({ 
    price: { $gt: 200 } 
  }).toArray();

  // $eq (equals)
  const electronics = await collection.find({ 
    category: { $eq: "Electronics" } 
  }).toArray();

  // $in (in array)
  const selected = await collection.find({ 
    category: { $in: ["Electronics", "Furniture"] } 
  }).toArray();

  // $and (logical AND)
  const filtered = await collection.find({ 
    $and: [
      { price: { $lt: 500 } }, 
      { stock: { $gt: 10 } }
    ] 
  }).toArray();

  await client.close();
}

queryExamples();

Understanding MongoDB query operators with practical examples