MongoDB CRUD Operations Comprehensive Guide to Database Operations

📚 MongoDB CRUD Operations Overview

CRUD Operations form the foundation of database interactions in MongoDB. These operations allow you to Create, Read, Update, and Delete documents within collections, providing the essential functionality needed for any data-driven application.

Key Concepts: Documents, Collections, BSON data types, Query operators, Aggregation framework, Indexing strategies, and Transaction management.

1. Database Setup & Schema Design

1.1 Database Initialization and Collection Creation

MongoDB Shell - Database Setup
// Switch to database (creates if doesn't exist)
use sampleDatabase

// Create collection with schema validation
db.createCollection("users", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["userId", "email", "firstName", "lastName", "createdAt"],
      properties: {
        userId: {
          bsonType: "string",
          pattern: "^USR[0-9]{6}$",
          description: "Unique user identifier format: USR######"
        },
        email: {
          bsonType: "string",
          pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
          description: "Valid email address format required"
        },
        firstName: {
          bsonType: "string",
          minLength: 1,
          maxLength: 50
        },
        lastName: {
          bsonType: "string",
          minLength: 1,
          maxLength: 50
        },
        age: {
          bsonType: "int",
          minimum: 13,
          maximum: 120,
          description: "User age between 13 and 120"
        },
        status: {
          enum: ["active", "inactive", "suspended", "pending"],
          description: "User account status"
        },
        createdAt: {
          bsonType: "date",
          description: "Account creation timestamp"
        }
      }
    }
  },
  validationLevel: "strict",
  validationAction: "error"
})

🔧 Schema Validation in MongoDB

JSON Schema Validation provides document structure enforcement at the database level. MongoDB 8.0 enhances validation capabilities with improved performance and extended operator support.

📚 Official Documentation: Schema Validation - MongoDB Manual
$jsonSchema Operator Reference
MongoDB Shell - Collection with Indexes
// Create products collection with validation
db.createCollection("products", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["productId", "name", "price", "category"],
      properties: {
        productId: {
          bsonType: "string",
          pattern: "^PRD[0-9]{6}$"
        },
        name: {
          bsonType: "string",
          minLength: 1,
          maxLength: 100
        },
        price: {
          bsonType: "decimal",
          minimum: 0,
          description: "Product price using Decimal128 for precision"
        },
        category: {
          bsonType: "string",
          enum: ["electronics", "clothing", "books", "home", "sports"]
        },
        tags: {
          bsonType: "array",
          items: { bsonType: "string" },
          maxItems: 10
        },
        inStock: {
          bsonType: "bool",
          description: "Availability status"
        },
        specifications: {
          bsonType: "object",
          description: "Product specifications as embedded document"
        }
      }
    }
  }
})

// Create orders collection for transaction examples
db.createCollection("orders", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["orderId", "userId", "items", "totalAmount", "orderDate"],
      properties: {
        orderId: {
          bsonType: "string",
          pattern: "^ORD[0-9]{8}$"
        },
        userId: {
          bsonType: "string",
          pattern: "^USR[0-9]{6}$"
        },
        items: {
          bsonType: "array",
          minItems: 1,
          items: {
            bsonType: "object",
            required: ["productId", "quantity", "price"],
            properties: {
              productId: { bsonType: "string" },
              quantity: { bsonType: "int", minimum: 1 },
              price: { bsonType: "decimal", minimum: 0 }
            }
          }
        },
        totalAmount: {
          bsonType: "decimal",
          minimum: 0
        },
        status: {
          enum: ["pending", "confirmed", "shipped", "delivered", "cancelled"]
        },
        orderDate: {
          bsonType: "date"
        }
      }
    }
  }
})

📊 Data Types and BSON Considerations

BSON (Binary JSON) extends JSON with additional data types optimized for storage and traversal. MongoDB 8.0 introduces enhanced BSON handling and new data type features.

1.2 Sample Data Insertion with Best Practices

MongoDB Shell - Sample Data Creation
// Insert sample users with comprehensive data
db.users.insertMany([
  {
    userId: "USR000001",
    email: "alice.johnson@example.com",
    firstName: "Alice",
    lastName: "Johnson",
    age: NumberInt(28),
    address: {
      street: "123 Tech Avenue",
      city: "San Francisco",
      state: "CA",
      zipCode: "94105",
      country: "USA"
    },
    preferences: {
      newsletter: true,
      notifications: true,
      language: "en",
      timezone: "America/Los_Angeles"
    },
    tags: ["premium", "early_adopter"],
    status: "active",
    lastLoginAt: new Date(),
    createdAt: new Date(),
    metadata: {
      source: "web_signup",
      referrer: "google",
      device: "desktop"
    }
  },
  {
    userId: "USR000002",
    email: "bob.smith@example.com",
    firstName: "Bob",
    lastName: "Smith",
    age: NumberInt(35),
    address: {
      street: "456 Business District",
      city: "New York",
      state: "NY",
      zipCode: "10001",
      country: "USA"
    },
    preferences: {
      newsletter: false,
      notifications: true,
      language: "en",
      timezone: "America/New_York"
    },
    tags: ["business", "frequent_buyer"],
    status: "active",
    lastLoginAt: new Date(),
    createdAt: new Date(),
    metadata: {
      source: "mobile_app",
      referrer: "direct",
      device: "mobile"
    }
  }
], {
  ordered: true,
  writeConcern: { w: "majority", j: true }
})

// Insert sample products
db.products.insertMany([
  {
    productId: "PRD000001",
    name: "Wireless Headphones",
    price: NumberDecimal("299.99"),
    category: "electronics",
    tags: ["audio", "wireless", "premium"],
    inStock: true,
    specifications: {
      brand: "TechCorp",
      model: "WH-1000XM4",
      color: "black",
      batteryLife: "30 hours",
      features: ["noise-cancelling", "bluetooth", "voice-assistant"]
    },
    ratings: {
      average: NumberDecimal("4.5"),
      count: NumberInt(1250)
    },
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    productId: "PRD000002",
    name: "Programming JavaScript Book",
    price: NumberDecimal("45.99"),
    category: "books",
    tags: ["programming", "javascript", "education"],
    inStock: true,
    specifications: {
      author: "Jane Developer",
      pages: NumberInt(450),
      publisher: "Tech Publications",
      isbn: "978-0123456789",
      language: "English"
    },
    ratings: {
      average: NumberDecimal("4.8"),
      count: NumberInt(892)
    },
    createdAt: new Date(),
    updatedAt: new Date()
  }
], { writeConcern: { w: "majority", j: true } })

💡 Write Concern and Data Durability

Write Concern controls the acknowledgment of write operations. MongoDB 8.0 provides enhanced write concern options for improved data safety and performance tuning.

📚 Official Documentation: Write Concern - MongoDB Manual
insertMany() Method Reference

2. CREATE Operations (Insert Patterns)

2.1 Single Document Insertion

MongoDB Shell - insertOne() Method
// Insert single document with comprehensive error handling
try {
  const result = db.users.insertOne({
    userId: "USR000003",
    email: "charlie.brown@example.com",
    firstName: "Charlie",
    lastName: "Brown",
    age: NumberInt(42),
    address: {
      street: "789 Innovation Boulevard",
      city: "Austin",
      state: "TX",
      zipCode: "78701",
      country: "USA"
    },
    preferences: {
      newsletter: true,
      notifications: false,
      language: "en",
      timezone: "America/Chicago"
    },
    tags: ["beta_tester", "feedback_provider"],
    status: "active",
    lastLoginAt: null, // Never logged in
    createdAt: new Date(),
    metadata: {
      source: "referral",
      referrer: "friend",
      device: "tablet",
      campaignId: "SUMMER2024"
    }
  }, {
    writeConcern: { w: "majority", j: true, wtimeout: 5000 }
  })

  print("Document inserted successfully:")
  print("Inserted ID:", result.insertedId)
  print("Acknowledged:", result.acknowledged)

} catch (error) {
  print("Insertion failed:", error.message)
  if (error.code === 11000) {
    print("Duplicate key error - document already exists")
  }
}

⚡ insertOne() Method Characteristics

Single document insertion provides atomic operation guarantees at the document level. MongoDB 8.0 enhances insertion performance with improved write path optimizations.

2.2 Bulk Insert Operations

MongoDB Shell - Bulk insertMany() Operations
// Bulk insert with unordered execution for performance
const orderBatch = [
  {
    orderId: "ORD00000001",
    userId: "USR000001",
    items: [
      {
        productId: "PRD000001",
        quantity: NumberInt(1),
        price: NumberDecimal("299.99")
      }
    ],
    totalAmount: NumberDecimal("299.99"),
    status: "confirmed",
    orderDate: new Date(),
    shippingAddress: {
      street: "123 Tech Avenue",
      city: "San Francisco",
      state: "CA",
      zipCode: "94105",
      country: "USA"
    },
    paymentMethod: {
      type: "credit_card",
      last4: "1234"
    }
  },
  {
    orderId: "ORD00000002",
    userId: "USR000002",
    items: [
      {
        productId: "PRD000002",
        quantity: NumberInt(2),
        price: NumberDecimal("45.99")
      },
      {
        productId: "PRD000001",
        quantity: NumberInt(1),
        price: NumberDecimal("299.99")
      }
    ],
    totalAmount: NumberDecimal("391.97"),
    status: "pending",
    orderDate: new Date(),
    shippingAddress: {
      street: "456 Business District",
      city: "New York",
      state: "NY",
      zipCode: "10001",
      country: "USA"
    },
    paymentMethod: {
      type: "paypal",
      email: "bob.smith@example.com"
    }
  }
]

try {
  const result = db.orders.insertMany(orderBatch, {
    ordered: false, // Continue processing on errors
    writeConcern: { w: "majority", j: true }
  })

  print("Bulk insert results:")
  print("Documents inserted:", result.insertedCount)
  print("Inserted IDs:", Object.keys(result.insertedIds).length)

} catch (BulkWriteError) {
  print("Bulk write completed with errors:")
  print("Successful inserts:", BulkWriteError.result.nInserted)
  print("Write errors:", BulkWriteError.writeErrors.length)

  // Log specific errors for debugging
  BulkWriteError.writeErrors.forEach((error, index) => {
    print(`Error ${index + 1}: ${error.errmsg}`)
    print(`Failed document index: ${error.index}`)
  })
}

🚀 Bulk Operations Performance

Bulk insert operations optimize network round trips and provide better throughput for large datasets. MongoDB 8.0 introduces enhanced bulk operation algorithms.

📚 Official Documentation: Bulk Write Operations - MongoDB Manual
insertMany() Method Reference

3. READ Operations (Query Patterns)

3.1 Basic Query Operations

MongoDB Shell - Basic find() Operations
// Find single document by unique identifier
db.users.findOne({ userId: "USR000001" })

// Find with multiple conditions and projection
db.users.find({
  status: "active",
  age: { $gte: 25, $lte: 40 }
}, {
  userId: 1,
  firstName: 1,
  lastName: 1,
  email: 1,
  age: 1,
  _id: 0
})

// Query embedded documents using dot notation
db.users.find({
  "address.state": "CA",
  "address.city": "San Francisco",
  "preferences.newsletter": true
})

// Array element queries
db.users.find({
  tags: { $in: ["premium", "early_adopter"] }
})

// Complex query with logical operators
db.products.find({
  $and: [
    { inStock: true },
    { price: { $lte: NumberDecimal("500.00") } },
    { category: { $in: ["electronics", "books"] } },
    { "ratings.average": { $gte: NumberDecimal("4.0") } }
  ]
}).sort({ price: 1 }).limit(10)

// Regular expression queries for text patterns
db.users.find({
  email: { $regex: /@example\.com$/, $options: "i" },
  firstName: { $regex: /^A/, $options: "i" }
})

🔍 Query Operator Fundamentals

MongoDB Query Language provides rich query operators for precise document retrieval. MongoDB 8.0 introduces new query operators and performance optimizations.

3.2 Advanced Query Patterns

MongoDB Shell - Advanced Query Operations
// Date range queries with ISODate
db.orders.find({
  orderDate: {
    $gte: ISODate("2024-01-01T00:00:00.000Z"),
    $lt: ISODate("2024-12-31T23:59:59.999Z")
  },
  status: { $ne: "cancelled" }
}).sort({ orderDate: -1 })

// Text search queries (requires text index)
db.products.find({
  $text: {
    $search: "wireless headphones",
    $caseSensitive: false,
    $language: "en"
  }
}, {
  score: { $meta: "textScore" }
}).sort({ score: { $meta: "textScore" } })

// Array queries with $elemMatch
db.orders.find({
  items: {
    $elemMatch: {
      quantity: { $gte: 2 },
      price: { $lte: NumberDecimal("100.00") }
    }
  }
})

// Exists and type queries
db.users.find({
  lastLoginAt: { $exists: true, $ne: null },
  age: { $type: "int" },
  "preferences.timezone": { $exists: true }
})

// Aggregation-style queries with $expr
db.products.find({
  $expr: {
    $gt: [
      { $multiply: ["$ratings.average", "$ratings.count"] },
      1000 // Products with high total rating points
    ]
  }
})

// Geospatial queries (assuming location field exists)
db.stores.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [-122.4194, 37.7749] // San Francisco
      },
      $maxDistance: 5000 // 5km radius
    }
  }
})

🌟 Advanced Query Capabilities

Advanced query patterns enable sophisticated data retrieval scenarios. MongoDB 8.0 enhances query execution with improved optimizer and new features.

3.3 Aggregation Pipeline Queries

MongoDB Shell - Aggregation Pipeline
// User order summary with aggregation pipeline
db.orders.aggregate([
  // Stage 1: Match completed orders
  {
    $match: {
      status: { $in: ["confirmed", "shipped", "delivered"] }
    }
  },

  // Stage 2: Join with user information
  {
    $lookup: {
      from: "users",
      localField: "userId",
      foreignField: "userId",
      as: "userInfo",
      pipeline: [
        {
          $project: {
            firstName: 1,
            lastName: 1,
            email: 1,
            "address.state": 1
          }
        }
      ]
    }
  },

  // Stage 3: Unwind user array
  {
    $unwind: "$userInfo"
  },

  // Stage 4: Unwind items for detailed analysis
  {
    $unwind: "$items"
  },

  // Stage 5: Join with product information
  {
    $lookup: {
      from: "products",
      localField: "items.productId",
      foreignField: "productId",
      as: "productInfo"
    }
  },

  // Stage 6: Unwind product array
  {
    $unwind: "$productInfo"
  },

  // Stage 7: Group by user with order statistics
  {
    $group: {
      _id: "$userId",
      customerName: {
        $first: {
          $concat: ["$userInfo.firstName", " ", "$userInfo.lastName"]
        }
      },
      email: { $first: "$userInfo.email" },
      state: { $first: "$userInfo.address.state" },
      totalSpent: { $sum: "$totalAmount" },
      orderCount: { $addToSet: "$orderId" },
      itemCount: { $sum: "$items.quantity" },
      categoriesPurchased: { $addToSet: "$productInfo.category" },
      avgOrderValue: { $avg: "$totalAmount" },
      lastOrderDate: { $max: "$orderDate" }
    }
  },

  // Stage 8: Add calculated fields
  {
    $addFields: {
      orderCount: { $size: "$orderCount" },
      categoryCount: { $size: "$categoriesPurchased" },
      customerTier: {
        $switch: {
          branches: [
            { case: { $gte: ["$totalSpent", NumberDecimal("1000.00")] }, then: "Gold" },
            { case: { $gte: ["$totalSpent", NumberDecimal("500.00")] }, then: "Silver" },
            { case: { $gte: ["$totalSpent", NumberDecimal("100.00")] }, then: "Bronze" }
          ],
          default: "Standard"
        }
      }
    }
  },

  // Stage 9: Sort by total spent
  {
    $sort: { totalSpent: -1 }
  },

  // Stage 10: Limit results
  {
    $limit: 20
  }
])

// Time-based aggregation for sales analysis
db.orders.aggregate([
  {
    $match: {
      orderDate: {
        $gte: ISODate("2024-01-01T00:00:00.000Z")
      },
      status: { $ne: "cancelled" }
    }
  },
  {
    $group: {
      _id: {
        year: { $year: "$orderDate" },
        month: { $month: "$orderDate" },
        status: "$status"
      },
      totalRevenue: { $sum: "$totalAmount" },
      orderCount: { $sum: 1 },
      avgOrderValue: { $avg: "$totalAmount" },
      maxOrderValue: { $max: "$totalAmount" }
    }
  },
  {
    $sort: {
      "_id.year": 1,
      "_id.month": 1,
      "_id.status": 1
    }
  }
])

📊 Aggregation Framework Power

Aggregation Pipeline provides powerful data processing capabilities for analytics and reporting. MongoDB 8.0 introduces new aggregation operators and performance improvements.

📚 Official Documentation: Aggregation Pipeline - MongoDB Manual
Aggregation Pipeline Operators

4. UPDATE Operations (Document Modification)

4.1 Single Document Updates

MongoDB Shell - updateOne() Operations
// Update single document with $set operator
db.users.updateOne(
  { userId: "USR000001" },
  {
    $set: {
      lastLoginAt: new Date(),
      "preferences.language": "es",
      status: "active"
    },
    $inc: {
      loginCount: 1
    },
    $push: {
      tags: "frequent_user"
    }
  },
  {
    writeConcern: { w: "majority", j: true }
  }
)

// Conditional update with array filters
db.orders.updateOne(
  {
    orderId: "ORD00000001",
    "items.productId": "PRD000001"
  },
  {
    $set: {
      "items.$.quantity": NumberInt(2),
      "items.$.lastModified": new Date()
    },
    $inc: {
      totalAmount: NumberDecimal("299.99")
    }
  }
)

// Update with upsert option
db.userPreferences.updateOne(
  { userId: "USR000001" },
  {
    $set: {
      userId: "USR000001",
      theme: "dark",
      notifications: {
        email: true,
        push: false,
        sms: true
      },
      updatedAt: new Date()
    },
    $setOnInsert: {
      createdAt: new Date(),
      version: 1
    }
  },
  {
    upsert: true,
    writeConcern: { w: "majority" }
  }
)

// Atomic increment operations
db.products.updateOne(
  { productId: "PRD000001" },
  {
    $inc: {
      "ratings.count": NumberInt(1),
      viewCount: NumberInt(1)
    },
    $set: {
      lastViewedAt: new Date()
    }
  }
)

⚛️ Atomic Update Operations

Document-level atomicity ensures consistent updates within single documents. MongoDB 8.0 enhances atomic operations with improved performance and new operators.

4.2 Multiple Document Updates

MongoDB Shell - updateMany() Operations
// Bulk update multiple documents
const updateResult = db.products.updateMany(
  {
    category: "electronics",
    inStock: true,
    price: { $lte: NumberDecimal("1000.00") }
  },
  {
    $set: {
      promotionActive: true,
      promotionEndDate: new Date(Date.now() + 30*24*60*60*1000), // 30 days
      updatedAt: new Date()
    },
    $inc: {
      promotionCount: 1
    }
  },
  {
    writeConcern: { w: "majority", j: true }
  }
)

print(`Updated ${updateResult.modifiedCount} products`)

// Update with aggregation pipeline (MongoDB 4.2+)
db.orders.updateMany(
  { status: "pending" },
  [
    {
      $set: {
        totalWithTax: {
          $multiply: ["$totalAmount", 1.08] // Add 8% tax
        },
        lastModified: new Date(),
        processingDays: {
          $dateDiff: {
            startDate: "$orderDate",
            endDate: new Date(),
            unit: "day"
          }
        }
      }
    }
  ]
)

// Conditional updates with $cond
db.users.updateMany(
  { status: "active" },
  [
    {
      $set: {
        membershipTier: {
          $cond: {
            if: { $gte: ["$age", 65] },
            then: "senior",
            else: {
              $cond: {
                if: { $gte: ["$age", 18] },
                then: "adult",
                else: "junior"
              }
            }
          }
        },
        updatedAt: new Date()
      }
    }
  ]
)

// Update documents based on array size
db.users.updateMany(
  {},
  [
    {
      $set: {
        tagCount: { $size: { $ifNull: ["$tags", []] } },
        hasMultipleTags: {
          $gt: [{ $size: { $ifNull: ["$tags", []] } }, 1]
        }
      }
    }
  ]
)

🔄 Advanced Update Patterns

Aggregation pipeline updates enable complex document transformations during update operations. MongoDB 8.0 expands pipeline update capabilities with new stages and operators.

📚 Official Documentation: updateOne() - MongoDB Manual
updateMany() - MongoDB Manual

5. DELETE Operations (Document Removal)

5.1 Single Document Deletion

MongoDB Shell - deleteOne() Operations
// Delete single document with specific criteria
const deleteResult = db.users.deleteOne({
  userId: "USR000003",
  status: "inactive"
})

if (deleteResult.deletedCount === 1) {
  print("User successfully deleted")
} else {
  print("No user found matching criteria")
}

// Soft delete pattern (recommended approach)
db.users.updateOne(
  { userId: "USR000003" },
  {
    $set: {
      status: "deleted",
      deletedAt: new Date(),
      deletedBy: "admin_user",
      deletedReason: "user_request"
    },
    $unset: {
      // Remove sensitive information
      email: "",
      "address.street": ""
    }
  }
)

// Create deletion audit record
db.deletionAudit.insertOne({
  documentType: "user",
  documentId: "USR000003",
  deletedAt: new Date(),
  deletedBy: "admin_user",
  reason: "user_request",
  recoverable: true,
  retentionPolicy: "7_years"
})

// Conditional deletion with validation
const userToDelete = db.users.findOne({ userId: "USR000003" })
if (userToDelete && userToDelete.status === "inactive") {
  const deleteResult = db.users.deleteOne({ userId: "USR000003" })
  print(`Deleted ${deleteResult.deletedCount} user(s)`)
} else {
  print("User not eligible for deletion")
}

🗂️ Document Deletion Strategies

Deletion patterns vary based on data retention requirements and business logic. MongoDB 8.0 provides enhanced deletion performance and audit capabilities.

5.2 Multiple Document Deletion

MongoDB Shell - deleteMany() Operations
// Delete multiple documents with date-based criteria
const cutoffDate = new Date(Date.now() - 90*24*60*60*1000) // 90 days ago

const deleteResult = db.orders.deleteMany({
  status: "cancelled",
  orderDate: { $lt: cutoffDate }
})

print(`Deleted ${deleteResult.deletedCount} cancelled orders`)

// Delete with complex criteria
db.products.deleteMany({
  $and: [
    { inStock: false },
    { "ratings.count": { $lt: 5 } },
    { createdAt: { $lt: new Date(Date.now() - 365*24*60*60*1000) } }
  ]
})

// Bulk soft delete with updateMany
db.users.updateMany(
  {
    lastLoginAt: { $lt: new Date(Date.now() - 2*365*24*60*60*1000) }, // 2 years
    status: "active"
  },
  {
    $set: {
      status: "archived",
      archivedAt: new Date(),
      archivedReason: "inactivity_2_years"
    }
  }
)

// Delete with limit (using aggregation for complex logic)
// First, find documents to delete
const documentsToDelete = db.logs.aggregate([
  {
    $match: {
      level: "debug",
      timestamp: { $lt: new Date(Date.now() - 7*24*60*60*1000) }
    }
  },
  { $limit: 1000 },
  { $project: { _id: 1 } }
]).toArray()

// Extract IDs and delete
const idsToDelete = documentsToDelete.map(doc => doc._id)
const batchDeleteResult = db.logs.deleteMany({
  _id: { $in: idsToDelete }
})

print(`Batch deleted ${batchDeleteResult.deletedCount} log entries`)

// Cleanup orphaned references
db.orderItems.deleteMany({
  orderId: {
    $nin: db.orders.distinct("orderId")
  }
})
⚠️ Deletion Best Practices: Always implement proper authorization checks, maintain audit trails, and consider soft deletion for critical business data. Use transactions when deleting related documents across collections.
📚 Official Documentation: deleteOne() - MongoDB Manual
deleteMany() - MongoDB Manual

6. ACID Transactions (Multi-Document Operations)

6.1 Basic Transaction Implementation

MongoDB Shell - Multi-Document Transactions
// Basic transaction with session management
function transferOrder(fromUserId, toUserId, orderId) {
  const session = db.getMongo().startSession({
    readConcern: { level: "snapshot" },
    writeConcern: { w: "majority", j: true }
  })

  try {
    session.startTransaction({
      readConcern: { level: "snapshot" },
      writeConcern: { w: "majority", j: true, wtimeout: 10000 }
    })

    const ordersCol = session.getDatabase("sampleDatabase").orders
    const usersCol = session.getDatabase("sampleDatabase").users

    // Step 1: Verify order exists and belongs to fromUser
    const order = ordersCol.findOne(
      {
        orderId: orderId,
        userId: fromUserId,
        status: "confirmed"
      },
      { session: session }
    )

    if (!order) {
      throw new Error("Order not found or not transferable")
    }

    // Step 2: Verify target user exists
    const targetUser = usersCol.findOne(
      { userId: toUserId, status: "active" },
      { session: session }
    )

    if (!targetUser) {
      throw new Error("Target user not found or inactive")
    }

    // Step 3: Update order ownership
    const updateResult = ordersCol.updateOne(
      {
        orderId: orderId,
        userId: fromUserId
      },
      {
        $set: {
          userId: toUserId,
          transferredFrom: fromUserId,
          transferredAt: new Date(),
          status: "transferred"
        }
      },
      { session: session }
    )

    if (updateResult.modifiedCount === 0) {
      throw new Error("Failed to transfer order")
    }

    // Step 4: Update user statistics
    usersCol.updateOne(
      { userId: fromUserId },
      {
        $inc: { ordersTransferred: 1 },
        $set: { lastActivity: new Date() }
      },
      { session: session }
    )

    usersCol.updateOne(
      { userId: toUserId },
      {
        $inc: { ordersReceived: 1 },
        $set: { lastActivity: new Date() }
      },
      { session: session }
    )

    // Commit transaction
    session.commitTransaction()

    print("Order transfer completed successfully")
    return { success: true, orderId: orderId }

  } catch (error) {
    print("Transaction failed:", error.message)
    session.abortTransaction()
    return { success: false, error: error.message }

  } finally {
    session.endSession()
  }
}

// Execute transaction
const result = transferOrder("USR000001", "USR000002", "ORD00000001")

🏛️ ACID Transaction Properties

Multi-document transactions provide ACID guarantees across multiple documents and collections. MongoDB 8.0 enhances transaction performance with improved concurrency control.

6.2 Complex Business Logic Transactions

MongoDB Shell - Complex Transaction Operations
// Complex order processing with inventory management
function processOrderWithInventory(userId, orderItems) {
  const session = db.getMongo().startSession()

  try {
    session.startTransaction({
      readConcern: { level: "snapshot" },
      writeConcern: { w: "majority", j: true }
    })

    const usersCol = session.getDatabase("sampleDatabase").users
    const productsCol = session.getDatabase("sampleDatabase").products
    const ordersCol = session.getDatabase("sampleDatabase").orders
    const inventoryCol = session.getDatabase("sampleDatabase").inventory

    // Step 1: Validate user
    const user = usersCol.findOne(
      { userId: userId, status: "active" },
      { session: session }
    )

    if (!user) {
      throw new Error("User not found or inactive")
    }

    let totalAmount = NumberDecimal("0.00")
    const processedItems = []

    // Step 2: Process each item
    for (const item of orderItems) {
      // Check product availability
      const product = productsCol.findOne(
        {
          productId: item.productId,
          inStock: true
        },
        { session: session }
      )

      if (!product) {
        throw new Error(`Product ${item.productId} not available`)
      }

      // Check inventory
      const inventory = inventoryCol.findOne(
        { productId: item.productId },
        { session: session }
      )

      if (!inventory || inventory.quantity < item.quantity) {
        throw new Error(`Insufficient inventory for ${item.productId}`)
      }

      // Calculate item total
      const itemTotal = product.price.multiply(NumberDecimal(item.quantity.toString()))
      totalAmount = totalAmount.add(itemTotal)

      // Reserve inventory
      const inventoryUpdate = inventoryCol.updateOne(
        {
          productId: item.productId,
          quantity: { $gte: item.quantity }
        },
        {
          $inc: {
            quantity: -item.quantity,
            reserved: item.quantity
          },
          $set: { lastUpdated: new Date() }
        },
        { session: session }
      )

      if (inventoryUpdate.modifiedCount === 0) {
        throw new Error(`Failed to reserve inventory for ${item.productId}`)
      }

      processedItems.push({
        productId: item.productId,
        quantity: NumberInt(item.quantity),
        price: product.price,
        total: itemTotal
      })
    }

    // Step 3: Create order
    const orderId = "ORD" + Date.now() + Math.random().toString(36).substr(2, 4).toUpperCase()

    const order = {
      orderId: orderId,
      userId: userId,
      items: processedItems,
      totalAmount: totalAmount,
      status: "confirmed",
      orderDate: new Date(),
      inventoryReserved: true,
      paymentStatus: "pending"
    }

    ordersCol.insertOne(order, { session: session })

    // Step 4: Update user statistics
    usersCol.updateOne(
      { userId: userId },
      {
        $inc: {
          totalOrders: 1,
          totalSpent: totalAmount
        },
        $set: { lastOrderDate: new Date() }
      },
      { session: session }
    )

    // Step 5: Create audit trail
    const auditCol = session.getDatabase("sampleDatabase").auditLogs
    auditCol.insertOne({
      eventType: "order_created",
      orderId: orderId,
      userId: userId,
      totalAmount: totalAmount,
      itemCount: processedItems.length,
      timestamp: new Date(),
      sessionId: session.getSessionId().toString()
    }, { session: session })

    session.commitTransaction()

    print("Order processed successfully:")
    print("Order ID:", orderId)
    print("Total Amount:", totalAmount.toString())

    return { success: true, orderId: orderId, totalAmount: totalAmount }

  } catch (error) {
    print("Order processing failed:", error.message)
    session.abortTransaction()
    return { success: false, error: error.message }

  } finally {
    session.endSession()
  }
}

// Execute complex transaction
const orderItems = [
  { productId: "PRD000001", quantity: 1 },
  { productId: "PRD000002", quantity: 2 }
]

const orderResult = processOrderWithInventory("USR000001", orderItems)

🔄 Transaction Best Practices

Complex transactions require careful design to ensure performance and avoid deadlocks. MongoDB 8.0 provides enhanced transaction monitoring and optimization features.

📚 Official Documentation: Transactions - MongoDB Manual
Production Considerations for Transactions

7. Indexing Strategies for Optimal Performance

7.1 Essential Index Types

MongoDB Shell - Index Creation
// Single field indexes
db.users.createIndex(
  { userId: 1 },
  {
    unique: true,
    name: "idx_user_id_unique",
    background: true
  }
)

db.users.createIndex(
  { email: 1 },
  {
    unique: true,
    name: "idx_email_unique",
    background: true,
    collation: { locale: "en", strength: 2 } // Case-insensitive
  }
)

// Compound indexes for query optimization
db.orders.createIndex(
  {
    userId: 1,
    status: 1,
    orderDate: -1
  },
  {
    name: "idx_orders_user_status_date",
    background: true
  }
)

// Multi-key index for array fields
db.users.createIndex(
  { tags: 1 },
  {
    name: "idx_user_tags",
    background: true
  }
)

// Embedded document indexes
db.users.createIndex(
  {
    "address.state": 1,
    "address.city": 1
  },
  {
    name: "idx_user_location",
    background: true
  }
)

// Partial indexes for specific subsets
db.products.createIndex(
  {
    price: 1,
    category: 1
  },
  {
    partialFilterExpression: {
      inStock: true,
      price: { $lt: NumberDecimal("1000.00") }
    },
    name: "idx_available_products_under_1000",
    background: true
  }
)

// Sparse indexes for optional fields
db.users.createIndex(
  { phoneNumber: 1 },
  {
    sparse: true,
    unique: true,
    name: "idx_phone_sparse"
  }
)

📈 Index Design Principles

Effective indexing is crucial for query performance at scale. MongoDB 8.0 introduces improved index algorithms and new index types for better performance.

7.2 Advanced Index Features

MongoDB Shell - Advanced Indexing
// Text indexes for full-text search
db.products.createIndex(
  {
    name: "text",
    "specifications.brand": "text",
    tags: "text"
  },
  {
    weights: {
      name: 10,
      "specifications.brand": 5,
      tags: 1
    },
    name: "idx_product_text_search",
    default_language: "english"
  }
)

// Geospatial indexes for location-based queries
db.stores.createIndex(
  { location: "2dsphere" },
  {
    name: "idx_store_location"
  }
)

// TTL indexes for automatic document expiration
db.sessions.createIndex(
  { createdAt: 1 },
  {
    expireAfterSeconds: 3600, // 1 hour
    name: "idx_session_ttl"
  }
)

// Hashed indexes for sharding
db.users.createIndex(
  { userId: "hashed" },
  {
    name: "idx_user_id_hashed"
  }
)

// Wildcard indexes for flexible schema queries (MongoDB 4.2+)
db.products.createIndex(
  { "specifications.$**": 1 },
  {
    name: "idx_product_specs_wildcard",
    background: true
  }
)

// Clustered indexes (MongoDB 5.3+)
db.createCollection("timeSeries", {
  clusteredIndex: {
    key: { _id: 1 },
    unique: true
  }
})

// Columnstore indexes for analytics (MongoDB 6.0+)
db.analytics.createIndex(
  { "$**": "columnstore" },
  {
    name: "idx_analytics_columnstore"
  }
)

🌟 Specialized Index Types

Advanced index features provide specialized functionality for different use cases. MongoDB 8.0 expands these capabilities with new index types and optimizations.

7.3 Index Performance Analysis

MongoDB Shell - Index Performance Monitoring
// Analyze query execution plans
db.orders.find({
  userId: "USR000001",
  status: "confirmed",
  orderDate: { $gte: ISODate("2024-01-01") }
}).explain("executionStats")

// Check index usage statistics
db.orders.aggregate([
  { $indexStats: {} }
]).forEach(stat => {
  print("Index:", stat.name)
  print("Operations:", stat.accesses.ops)
  print("Since:", stat.accesses.since)
  print("---")
})

// Identify slow queries using profiler
db.setProfilingLevel(2, {
  slowms: 100,  // Operations slower than 100ms
  sampleRate: 0.1  // Sample 10% of operations
})

// Query profile collection for analysis
db.system.profile.find({
  "command.find": "orders",
  millis: { $gt: 100 }
}).sort({ ts: -1 }).limit(5).forEach(op => {
  print("Collection:", op.command.find || op.command.aggregate)
  print("Duration (ms):", op.millis)
  print("Docs examined:", op.docsExamined)
  print("Docs returned:", op.docsReturned)
  print("Index used:", op.planSummary)
  print("---")
})

// Check collection and index statistics
const stats = db.orders.stats()
print("Collection Statistics:")
print("Document count:", stats.count)
print("Average document size:", stats.avgObjSize)
print("Total size:", stats.size)
print("Index count:", stats.nindexes)
print("Total index size:", stats.totalIndexSize)

// List all indexes with details
db.orders.getIndexes().forEach(index => {
  print("Index:", index.name)
  print("Key:", JSON.stringify(index.key))
  print("Unique:", index.unique || false)
  print("Sparse:", index.sparse || false)
  print("---")
})

// Hint query to use specific index
db.orders.find({
  userId: "USR000001",
  orderDate: { $gte: ISODate("2024-01-01") }
}).hint("idx_orders_user_status_date")

// Get query plan cache information
db.orders.getPlanCache().listQueryShapes().forEach(shape => {
  print("Query pattern:", JSON.stringify(shape.query))
  print("Sort pattern:", JSON.stringify(shape.sort))
  print("Projection:", JSON.stringify(shape.projection))
  print("---")
})

// Clear plan cache for testing
db.runCommand({ planCacheClear: "orders" })

📊 Performance Monitoring Best Practices

Index monitoring ensures optimal query performance as data grows. MongoDB 8.0 provides enhanced monitoring tools and query optimization features.

📚 Official Documentation: Indexes - MongoDB Manual
Analyze Query Performance

8. Advanced MongoDB Concepts

8.1 Data Modeling Patterns

MongoDB Shell - Data Modeling Patterns
// Embedded Document Pattern
db.blogPosts.insertOne({
  title: "MongoDB Best Practices",
  content: "Content here...",
  author: {
    name: "John Developer",
    email: "john@example.com",
    bio: "Senior MongoDB Developer"
  },
  tags: ["mongodb", "database", "nosql"],
  comments: [
    {
      author: "Alice",
      content: "Great article!",
      timestamp: new Date(),
      likes: 5
    },
    {
      author: "Bob",
      content: "Very helpful",
      timestamp: new Date(),
      likes: 3
    }
  ],
  createdAt: new Date(),
  updatedAt: new Date()
})

// Reference Pattern for normalized data
db.authors.insertOne({
  authorId: "AUTH001",
  name: "John Developer",
  email: "john@example.com",
  bio: "Senior MongoDB Developer",
  socialMedia: {
    twitter: "@johndev",
    linkedin: "linkedin.com/in/johndev"
  }
})

db.blogPosts.insertOne({
  title: "Advanced MongoDB Patterns",
  content: "Content here...",
  authorId: "AUTH001", // Reference to authors collection
  tags: ["mongodb", "advanced", "patterns"],
  createdAt: new Date()
})

// Extended Reference Pattern with frequently accessed data
db.blogPosts.insertOne({
  title: "MongoDB Indexing Guide",
  content: "Content here...",
  author: {
    authorId: "AUTH001",
    name: "John Developer", // Denormalized for quick access
    email: "john@example.com"
  },
  tags: ["mongodb", "indexing"],
  createdAt: new Date()
})

// Bucket Pattern for time-series data
db.sensorData.insertOne({
  sensorId: "SENSOR001",
  bucketDate: ISODate("2024-07-24T00:00:00Z"),
  readings: [
    {
      timestamp: ISODate("2024-07-24T00:00:00Z"),
      temperature: 23.5,
      humidity: 65.2
    },
    {
      timestamp: ISODate("2024-07-24T00:05:00Z"),
      temperature: 23.7,
      humidity: 65.0
    }
    // ... more readings for this hour
  ],
  readingCount: 12,
  avgTemperature: 23.6,
  avgHumidity: 65.1
})

// Polymorphic Pattern for varied document structures
db.vehicles.insertMany([
  {
    vehicleId: "VEH001",
    type: "car",
    brand: "Toyota",
    model: "Camry",
    year: 2023,
    doors: 4,
    fuelType: "gasoline"
  },
  {
    vehicleId: "VEH002",
    type: "motorcycle",
    brand: "Honda",
    model: "CBR600RR",
    year: 2023,
    engineSize: "600cc",
    bikeType: "sport"
  },
  {
    vehicleId: "VEH003",
    type: "truck",
    brand: "Ford",
    model: "F-150",
    year: 2023,
    payload: 1500,
    bedLength: "6.5ft"
  }
])

🏗️ Data Modeling Strategies

Data modeling in MongoDB balances query performance, data consistency, and application requirements. Choose patterns based on access patterns and relationships.

8.2 Aggregation Framework Advanced Features

MongoDB Shell - Advanced Aggregation
// Window functions for analytics (MongoDB 5.0+)
db.orders.aggregate([
  {
    $setWindowFields: {
      partitionBy: "$userId",
      sortBy: { orderDate: 1 },
      output: {
        runningTotal: {
          $sum: "$totalAmount",
          window: {
            documents: ["unbounded", "current"]
          }
        },
        orderRank: {
          $rank: {}
        },
        avgOrderLast3: {
          $avg: "$totalAmount",
          window: {
            documents: [-2, "current"]
          }
        }
      }
    }
  },
  {
    $match: {
      orderRank: { $lte: 5 } // Top 5 orders per user
    }
  }
])

// Time series aggregation with date operators
db.orders.aggregate([
  {
    $match: {
      orderDate: {
        $gte: ISODate("2024-01-01"),
        $lt: ISODate("2025-01-01")
      }
    }
  },
  {
    $group: {
      _id: {
        year: { $year: "$orderDate" },
        month: { $month: "$orderDate" },
        week: { $week: "$orderDate" }
      },
      totalRevenue: { $sum: "$totalAmount" },
      orderCount: { $sum: 1 },
      avgOrderValue: { $avg: "$totalAmount" },
      uniqueCustomers: { $addToSet: "$userId" }
    }
  },
  {
    $addFields: {
      uniqueCustomerCount: { $size: "$uniqueCustomers" },
      revenuePerCustomer: {
        $divide: ["$totalRevenue", { $size: "$uniqueCustomers" }]
      }
    }
  },
  {
    $sort: {
      "_id.year": 1,
      "_id.month": 1,
      "_id.week": 1
    }
  }
])

// Complex lookup with pipeline
db.users.aggregate([
  {
    $lookup: {
      from: "orders",
      let: { userId: "$userId" },
      pipeline: [
        {
          $match: {
            $expr: { $eq: ["$userId", "$userId"] },
            status: { $in: ["confirmed", "delivered"] }
          }
        },
        {
          $group: {
            _id: null,
            totalSpent: { $sum: "$totalAmount" },
            orderCount: { $sum: 1 },
            avgOrderValue: { $avg: "$totalAmount" },
            lastOrderDate: { $max: "$orderDate" }
          }
        }
      ],
      as: "orderStats"
    }
  },
  {
    $unwind: {
      path: "$orderStats",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $addFields: {
      customerTier: {
        $switch: {
          branches: [
            {
              case: { $gte: [{ $ifNull: ["$orderStats.totalSpent", 0] }, 1000] },
              then: "Gold"
            },
            {
              case: { $gte: [{ $ifNull: ["$orderStats.totalSpent", 0] }, 500] },
              then: "Silver"
            }
          ],
          default: "Bronze"
        }
      },
      daysSinceLastOrder: {
        $dateDiff: {
          startDate: { $ifNull: ["$orderStats.lastOrderDate", "$createdAt"] },
          endDate: new Date(),
          unit: "day"
        }
      }
    }
  }
])

// Graph lookup for hierarchical data
db.employees.aggregate([
  {
    $graphLookup: {
      from: "employees",
      startWith: "$managerId",
      connectFromField: "managerId",
      connectToField: "employeeId",
      as: "managementChain",
      maxDepth: 5
    }
  },
  {
    $addFields: {
      managementLevels: { $size: "$managementChain" }
    }
  }
])

// Faceted search for multiple aggregations
db.products.aggregate([
  {
    $facet: {
      categoryCounts: [
        {
          $group: {
            _id: "$category",
            count: { $sum: 1 },
            avgPrice: { $avg: "$price" }
          }
        }
      ],
      priceRanges: [
        {
          $bucket: {
            groupBy: "$price",
            boundaries: [0, 50, 100, 500, 1000, 5000],
            default: "Other",
            output: {
              count: { $sum: 1 },
              products: { $push: "$name" }
            }
          }
        }
      ],
      topRated: [
        {
          $match: { "ratings.average": { $gte: 4.5 } }
        },
        {
          $sort: { "ratings.average": -1 }
        },
        {
          $limit: 10
        }
      ]
    }
  }
])

🔧 Advanced Aggregation Capabilities

Advanced aggregation features enable complex analytics and data processing. MongoDB 8.0 introduces new aggregation operators and performance optimizations.

📚 Official Documentation: Data Modeling - MongoDB Manual
Aggregation Framework

9. MongoDB 8.0 New Features & Enhancements

MongoDB Shell - MongoDB 8.0 Features
// Enhanced query optimization and performance
// MongoDB 8.0 introduces improved query planner
db.products.find({
  $and: [
    { category: "electronics" },
    { price: { $gte: 100, $lte: 500 } },
    { inStock: true }
  ]
}).explain("queryPlanner")

// Improved aggregation performance
db.orders.aggregate([
  {
    $match: {
      orderDate: { $gte: ISODate("2024-01-01") }
    }
  },
  {
    $group: {
      _id: "$userId",
      totalSpent: { $sum: "$totalAmount" },
      orderCount: { $sum: 1 }
    }
  },
  {
    $sort: { totalSpent: -1 }
  }
], { allowDiskUse: true })

// Enhanced time series collections (MongoDB 8.0)
db.createCollection("metrics", {
  timeseries: {
    timeField: "timestamp",
    metaField: "metadata",
    granularity: "seconds",
    bucketMaxSpanSeconds: 3600 // 1 hour buckets
  }
})

// Insert time series data
db.metrics.insertMany([
  {
    timestamp: new Date(),
    metadata: { sensor: "temp_01", location: "warehouse_a" },
    value: 23.5,
    unit: "celsius"
  },
  {
    timestamp: new Date(),
    metadata: { sensor: "humidity_01", location: "warehouse_a" },
    value: 65.2,
    unit: "percent"
  }
])

// Enhanced change streams with improved filtering
const changeStream = db.orders.watch([
  {
    $match: {
      "fullDocument.totalAmount": { $gte: 1000 },
      operationType: { $in: ["insert", "update"] }
    }
  }
], {
  fullDocument: "updateLookup",
  fullDocumentBeforeChange: "whenAvailable"
})

changeStream.on("change", (change) => {
  print("High-value order change detected:")
  printjson(change)
})

// Improved validation with custom expressions
db.createCollection("advancedValidation", {
  validator: {
    $expr: {
      $and: [
        { $gte: ["$endDate", "$startDate"] },
        {
          $or: [
            { $eq: ["$status", "draft"] },
            { $ne: ["$approvedBy", null] }
          ]
        }
      ]
    }
  }
})

// Enhanced connection pooling and monitoring
db.runCommand({
  serverStatus: 1,
  connections: 1,
  network: 1
})

// Queryable encryption (MongoDB Enterprise 8.0)
// Note: Requires proper key management setup
const encryptedFields = {
  "personalInfo.ssn": {
    keyId: UUID("12345678-1234-5678-9abc-123456789012"),
    bsonType: "string",
    queries: { queryType: "equality" }
  }
}

// Create collection with queryable encryption
db.createCollection("sensitiveData", {
  encryptedFields: encryptedFields
})

🚀 MongoDB 8.0 Key Enhancements

MongoDB 8.0 introduces significant performance improvements, enhanced security features, and new capabilities for modern applications.

10. Best Practices Summary

✅ MongoDB CRUD Operations Best Practices:

🎯 Production Deployment Considerations

Enterprise MongoDB Deployment: Key considerations for production environments:

📚 Essential MongoDB Resources:

📋 CRUD Operations Summary

This comprehensive guide covers all essential MongoDB operations:

  • CREATE: insertOne(), insertMany(), bulk operations
  • READ: find(), findOne(), aggregation pipelines
  • UPDATE: updateOne(), updateMany(), upsert operations
  • DELETE: deleteOne(), deleteMany(), soft delete patterns
  • Transactions: Multi-document ACID operations
  • Indexing: Performance optimization strategies
  • Advanced Features: Aggregation, text search, geospatial
  • MongoDB 8.0: Latest features and enhancements