Amazon DocumentDB (with MongoDB compatibility)

Hands-On

Demo

In this demo, we will:

  1. Create an Amazon DocumentDB cluster with read replicas
  2. Create an EC2 Machine for database access
  3. Connect to DocumentDB and create a product catalog database
  4. Perform CRUD operations and queries on product data
  5. Clean up resources

Agenda

Step 1: Create Amazon DocumentDB Cluster

Create Amazon DocumentDB cluster

product-catalog-cluster

Instance configuration

Cluster storage configuration

Connectivity

Authentication

docdbadmin

Network settings

Cluster options

Encryption-at-rest

Performance Insights

Log exports

Maintenance

Tags

Deletion protection

Step 2: Create EC2 Machine to Connect to DocumentDB

Create instance

documentdb-client

Launch an instance

Instance type

Key pair (login)

Network settings

Configure storage

IAM instance profile

Step 3 : Connect to DocumentDB from EC2

Connect

sudo -i
# Download MongoDB shell
wget https://downloads.mongodb.com/compass/mongodb-mongosh-1.10.0.x86_64.rpm
# Install the shell
sudo yum install -y mongodb-mongosh-1.10.0.x86_64.rpm
mongosh --version
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem

Step 4 : Create Collection and Perform CRUD Operations

// Switch to product catalog database
use productCatalog

// Create products collection with validation
db.createCollection("products", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: ["name", "category", "price", "inventory"],
         properties: {
            name: {
               bsonType: "string",
               description: "Product name is required"
            },
            category: {
               bsonType: "string",
               description: "Product category is required"
            },
            price: {
               bsonType: "decimal",
               minimum: 0,
               description: "Price must be a positive decimal"
            },
            inventory: {
               bsonType: "int",
               minimum: 0,
               description: "Inventory must be non-negative integer"
            }
         }
      }
   }
})

Create the product catalog database and collection

// Insert multiple products
db.products.insertMany([
   {
      name: "Wireless Headphones",
      category: "Electronics",
      subcategory: "Audio",
      price: NumberDecimal("79.99"),
      inventory: NumberInt(150),
      brand: "TechAudio",
      features: ["Bluetooth 5.0", "Noise Cancellation", "30-hour battery"],
      ratings: {
         average: 4.5,
         count: 234
      },
      dateAdded: new Date()
   },
   {
      name: "Yoga Mat Premium",
      category: "Sports",
      subcategory: "Fitness",
      price: NumberDecimal("29.99"),
      inventory: NumberInt(85),
      brand: "FitGear",
      features: ["Non-slip", "Extra thick", "Eco-friendly"],
      ratings: {
         average: 4.8,
         count: 89
      },
      dateAdded: new Date()
   },
   {
      name: "Smart Watch Pro",
      category: "Electronics",
      subcategory: "Wearables",
      price: NumberDecimal("249.99"),
      inventory: NumberInt(42),
      brand: "TechTime",
      features: ["Heart rate monitor", "GPS", "Water resistant"],
      ratings: {
         average: 4.3,
         count: 567
      },
      dateAdded: new Date()
   },
   {
      name: "Organic Coffee Beans",
      category: "Food",
      subcategory: "Beverages",
      price: NumberDecimal("15.99"),
      inventory: NumberInt(200),
      brand: "GreenBean",
      features: ["Fair trade", "Single origin", "Medium roast"],
      ratings: {
         average: 4.7,
         count: 123
      },
      dateAdded: new Date()
   }
])

Insert sample product data

// Create single field indexes
db.products.createIndex({ category: 1 })
db.products.createIndex({ price: 1 })
db.products.createIndex({ "ratings.average": -1 })

// Create compound index
db.products.createIndex({ category: 1, price: -1 })

// Create text index for search
db.products.createIndex({ name: "text", brand: "text" })

// View all indexes
db.products.getIndexes()

Create indexes for performance optimization

// Find all electronics under $100
db.products.find({
   category: "Electronics",
   price: { $lt: 100 }
}).pretty()

// Find top-rated products (rating > 4.5)
db.products.find({
   "ratings.average": { $gt: 4.5 }
}).sort({ "ratings.average": -1 }).pretty()

// Text search
db.products.find({
   $text: { $search: "Smart" }
}).pretty()

// Aggregation pipeline - Category summary
db.products.aggregate([
   {
      $group: {
         _id: "$category",
         avgPrice: { $avg: "$price" },
         totalInventory: { $sum: "$inventory" },
         productCount: { $sum: 1 }
      }
   },
   {
      $sort: { totalInventory: -1 }
   }
])

Perform queries

// Update single document - Apply discount
db.products.updateOne(
   { name: "Wireless Headphones" },
   {
      $mul: { price: 0.9 },  // 10% discount
      $set: { onSale: true, lastModified: new Date() }
   }
)

// Update multiple documents - Increase inventory
db.products.updateMany(
   { category: "Electronics" },
   { $inc: { inventory: 10 } }
)

Update operations

Clean Up

Delete DocumentDB Cluster

Terminate (delete) instance

🙏

Thanks

for

Watching

Amazon DocumentDB (with MongoDB compatibility) - Hands-On Demo - V2

By Deepak Dubey

Amazon DocumentDB (with MongoDB compatibility) - Hands-On Demo - V2

Amazon DocumentDB (with MongoDB compatibility) - Hands-On Demo - V2

  • 46