Master MongoDB data modeling, schema design, and document structure. Expert in relationships, embedding vs. referencing, normalization/denormalization, design patterns, and evolution strategies for scalable data models.
Master MongoDB schema design and document modeling for scalable applications. Learn embedding vs. referencing strategies, relationship patterns (1-1, 1-many, many-many), and advanced design patterns like polymorphic, attribute, and time-series models. Make informed trade-offs between normalization and denormalization for optimal query performance.
/plugin marketplace add pluginagentmarketplace/custom-plugin-mongodb/plugin install mongodb-developer-plugin@pluginagentmarketplace-mongodbsonnetMaster flexible, scalable schema design for optimal performance and maintainability.
This agent specializes in MongoDB data modeling and schema design, helping you architect databases that support your application's access patterns while maintaining data integrity and performance. Learn embedding vs. referencing decisions, relationship patterns, schema normalization, denormalization strategies, and advanced design patterns.
You'll learn: Document structure, relationships (1-1, 1-many, many-many), embedding/referencing strategies, schema patterns (attribute, polymorphic, versioned, outlier, tree, time-series), normalization/denormalization trade-offs, and schema evolution.
Focus on basic document structure and simple relationships:
Example: User with embedded address
db.users.insertOne({
_id: ObjectId(),
name: "John Doe",
email: "john@example.com",
createdAt: ISODate(),
address: {
street: "123 Main St",
city: "New York",
state: "NY",
zip: "10001"
}
})
Master relationship patterns and embedded vs. referenced decisions:
Example: Blog with posts and comments (1-many)
// Posts collection
db.posts.insertOne({
_id: ObjectId(),
title: "MongoDB Guide",
authorId: ObjectId("user_id"), // Reference
content: "...",
tags: ["mongodb", "database"], // Embed array
createdAt: ISODate(),
stats: { views: 1000, likes: 50 } // Denormalized
})
// Comments reference posts
db.comments.insertOne({
_id: ObjectId(),
postId: ObjectId("post_id"),
authorId: ObjectId("user_id"),
content: "Great article!",
createdAt: ISODate()
})
Architect complex, scalable schemas with advanced patterns:
Example: Polymorphic events collection
db.events.insertMany([
{
_id: ObjectId(),
type: "user_signup",
userId: ObjectId(),
timestamp: ISODate(),
data: { email: "user@example.com", source: "organic" }
},
{
_id: ObjectId(),
type: "purchase",
userId: ObjectId(),
timestamp: ISODate(),
data: { productId: ObjectId(), amount: 99.99, orderId: ObjectId() }
},
{
_id: ObjectId(),
type: "page_view",
userId: ObjectId(),
timestamp: ISODate(),
data: { page: "/products", duration: 30000, source: "referrer" }
}
])
// Query events by type
db.events.find({ type: "purchase", "data.amount": { $gt: 100 } })
Design products with flexible attributes, categories, and reviews:
db.products.insertOne({
_id: ObjectId(),
name: "Laptop",
sku: "LP-2024-001",
categoryId: ObjectId("category_id"),
price: 999.99,
attributes: { // Attribute pattern for flexible fields
brand: "Dell",
processor: "Intel i7",
ram: "16GB",
storage: "512GB SSD"
},
reviews: [ // Embed recent reviews
{ userId: ObjectId(), rating: 5, text: "Great laptop!" }
],
inventory: {
total: 100,
available: 95,
reserved: 5
},
createdAt: ISODate(),
updatedAt: ISODate()
})
Design isolated tenant data with shared core resources:
db.organizations.insertOne({
_id: ObjectId(),
name: "Acme Corp",
plan: "enterprise",
settings: {
features: ["analytics", "reporting", "api-access"],
seatLimit: 100,
dataRetention: 365
}
})
db.projects.insertOne({
_id: ObjectId(),
organizationId: ObjectId("org_id"),
name: "Q1 Campaign",
members: [ObjectId("user_id1"), ObjectId("user_id2")],
created At: ISODate()
})
// Query: Find all projects for an organization
db.projects.find({ organizationId: ObjectId("org_id") })
Model users, relationships, and activity streams:
db.users.insertOne({
_id: ObjectId(),
username: "johndoe",
profile: {
firstName: "John",
lastName: "Doe",
bio: "Developer",
avatar: "https://..."
},
stats: {
followers: 1000,
following: 500,
posts: 150
},
createdAt: ISODate()
})
db.posts.insertOne({
_id: ObjectId(),
authorId: ObjectId("user_id"),
content: "Just launched my project!",
likes: [ObjectId("user1"), ObjectId("user2")],
comments: [
{ userId: ObjectId(), text: "Awesome!", timestamp: ISODate() }
],
createdAt: ISODate()
})
Design flexible content with versioning and history:
db.articles.insertOne({
_id: ObjectId(),
title: "MongoDB Guide",
slug: "mongodb-guide",
author: ObjectId("user_id"),
status: "published",
content: "...",
metadata: {
tags: ["mongodb", "database"],
category: "tutorials",
seoKeywords: "mongodb tutorial"
},
version: 3,
versionHistory: [
{ version: 2, title: "MongoDB Beginner Guide", timestamp: ISODate() },
{ version: 1, title: "MongoDB Learning", timestamp: ISODate() }
],
publishedAt: ISODate(),
updatedAt: ISODate()
})
// ❌ Wrong: Comments array grows indefinitely
db.posts.insertOne({
title: "Post",
comments: [/* 100k items */] // Hits 16MB limit!
})
// ✅ Correct: Reference comments separately
db.posts.insertOne({ _id: ObjectId(), title: "Post" })
db.comments.insertOne({ postId: ObjectId("post_id"), text: "..." })
db.posts.aggregate([
{ $match: { _id: ObjectId("post_id") } },
{ $lookup: { from: "comments", localField: "_id", foreignField: "postId", as: "comments" } }
])
// ❌ Wrong: Full user object in every comment (duplicate data)
db.comments.insertOne({
text: "Great post!",
user: {
_id: ObjectId(),
name: "John",
email: "john@example.com",
profile: { ... } // Duplicated!
}
})
// ✅ Correct: Reference user, embed only needed data
db.comments.insertOne({
text: "Great post!",
userId: ObjectId("user_id"),
author: "John" // Cached for display
})
// ❌ Wrong: Unbounded growth of subdocument
db.analytics.insertOne({
_id: ObjectId(),
events: [] // Grows over time without limit
})
for (let i = 0; i < 1000000; i++) {
db.analytics.updateOne({ _id }, { $push: { events: { ... } } })
}
// ✅ Correct: Bucketing for time-series data
db.analytics.insertOne({
_id: "daily_2024_01_01",
date: ISODate("2024-01-01"),
events: [/* events for this day */],
count: 10000
})
// ❌ Wrong: Denormalized data gets stale
db.orders.insertOne({
userId: ObjectId(),
userName: "John", // What if name changes?
userEmail: "john@example.com" // Now outdated!
})
// ✅ Correct: Denormalize only for rarely-changing data
db.orders.insertOne({
userId: ObjectId(),
total: 99.99, // Immutable after order
taxRate: 0.08, // OK to denormalize
userEmail: "john@example.com" // Snapshot at order time
})
// ❌ Wrong: Too much nesting without $lookup plan
db.users.insertOne({
name: "John",
department: {
name: "Engineering",
manager: { name: "Jane", ... },
budget: { ... }
}
})
// ✅ Correct: Reference when complexity increases
db.users.insertOne({ name: "John", departmentId: ObjectId() })
db.departments.insertOne({ _id: ObjectId(), name: "Engineering", managerId: ObjectId() })
Q: Should I always embed documents? A: No, embed only for 1-few relationships with bounded data. Use references for 1-many and many-many.
Q: How do I handle many-to-many relationships? A: Use an array of IDs on both sides, a junction collection, or denormalized arrays depending on cardinality.
Q: Can I change my schema after creating collections? A: Yes, MongoDB allows schema evolution. Use schema validation and carefully plan migrations.
Q: What's the best way to denormalize data? A: Denormalize rarely-changing data for query performance. Plan how you'll keep denormalized values in sync.
Q: How do I handle large arrays without hitting the 16MB limit? A: Use referencing instead of embedding, or implement pagination/bucketing patterns.
Q: Should I use transactions for data consistency? A: For multi-document updates with referencing, yes use transactions. With embedding, single documents are atomic.
Q: How do I evolve my schema over time?
A: Add a version field, handle multiple versions in code, and migrate data gradually.
Ready to architect scalable MongoDB schemas! 🏗️
You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.