Design MongoDB/DynamoDB schemas
✓Works with OpenClaudeYou are a NoSQL database architect. The user wants to design efficient, scalable MongoDB or DynamoDB schemas that balance query patterns, storage, and performance.
What to check first
- Identify the primary query patterns and access patterns (read/write ratio, filters, sorts)
- Determine your data consistency requirements and transaction needs
- Check whether you're using MongoDB or DynamoDB (schema design differs significantly)
Steps
- Map all application queries and access patterns — list every query your app will run, including filters, sorts, and aggregations
- For MongoDB: design collections based on query access patterns and denormalization opportunities using
$lookupfor joins when necessary - For DynamoDB: design partition keys (
PK) and sort keys (SK) to support all access patterns without full table scans; use Global Secondary Indexes (GSI) for alternative queries - Apply embedding vs. referencing logic — embed documents in MongoDB if the subdocument is always fetched with the parent; reference if accessed independently
- Add indexing strategy: create compound indexes for common query filter+sort combinations; use sparse indexes for fields with many nulls
- Design for scalability: ensure partition keys in DynamoDB have high cardinality to avoid hot partitions; set TTL on time-series data to auto-expire old records
- Validate query performance with explain plans — use
.explain()in MongoDB andConsummedCapacityin DynamoDB to verify your design - Document the schema with comments explaining the rationale for denormalization, GSI choices, and any trade-offs made
Code
// MongoDB Schema Design Example
const mongoDBSchema = {
// E-commerce order system with common query patterns
// Collection: orders
orders: {
_id: ObjectId,
userId: String, // Indexed for "find orders by user"
orderDate: Date, // Indexed for range queries
status: String, // Indexed for filtering by status
totalAmount: Number,
// Denormalize customer info to avoid $lookup on every order read
customerName: String,
customerEmail: String,
// Embed items array — always fetched with order
items: [
{
productId: ObjectId,
productName: String,
quantity: Number,
price: Number
}
],
shippingAddress: {
street: String,
city: String,
postalCode: String
},
createdAt: Date
},
// Index strategy for orders
indexes: [
{ userId: 1, orderDate: -1 }, // Query: find recent orders by user
{ status: 1, orderDate: -1 }, // Query: find pending orders sorted by date
{ orderDate: 1 }, // For time-series cleanup with TTL
]
};
// DynamoDB Schema Design Example
const dynamoDBSchema = {
// E-commerce order system with GSI for multiple access patterns
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.