In this demo, we will:
documentdb-demo-vpc
10.0.0.0/16
documentdb-subnet-group
Subnet group for DocumentDB demo
documentdb-demo-vpc
documentdb-sg
Security group for DocumentDB cluster
documentdb-demo-vpc
documentdb-client-sg
Security group for DocumentDB clients
documentdb-demo-vpc
product-catalog-cluster
docdbadmin
documentdb-demo-vpc
documentdb-subnet-group
documentdb-sg
27017
documentdb-client
Cloud9 environment for DocumentDB access
documentdb-demo-vpc
Subnet: Select a public subnet
# 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
CLUSTER_ENDPOINT=
mongosh --host ${CLUSTER_ENDPOINT}:27017 \
--username docdbadmin \
--password docdbadmin \
--tls \
--tlsCAFile global-bundle.pem
// 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"
}
}
}
}
})
// 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()
}
])
// 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()
// 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 }
}
])
// 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 } }
)