mongodb-patterns
Document modeling, aggregation pipeline, indexing strategy, change streams, and multi-document transactions.
Best use case
mongodb-patterns is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Document modeling, aggregation pipeline, indexing strategy, change streams, and multi-document transactions.
Teams using mongodb-patterns should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/mongodb-patterns/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How mongodb-patterns Compares
| Feature / Agent | mongodb-patterns | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Document modeling, aggregation pipeline, indexing strategy, change streams, and multi-document transactions.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# MongoDB Patterns
Document database design and query optimization for MongoDB.
## Document Modeling Strategies
```typescript
// EMBED when: 1:1 or 1:few, data read together, child has no independent lifecycle
interface Order {
_id: ObjectId
customerId: ObjectId
status: 'pending' | 'paid' | 'shipped'
items: OrderItem[] // Embedded - always read with order
shippingAddress: Address // Embedded - 1:1
createdAt: Date
}
interface OrderItem {
productId: ObjectId
name: string // Denormalized - avoid join at read time
price: number // Snapshot at purchase time
quantity: number
}
// REFERENCE when: 1:many (unbounded), independent queries, shared across documents
interface Product {
_id: ObjectId
name: string
price: number
categoryId: ObjectId // Reference - category queried independently
reviews: never // DON'T embed - unbounded array
}
// Bucket pattern: group time-series data into fixed-size documents
interface SensorBucket {
_id: ObjectId
sensorId: string
startTime: Date
endTime: Date
count: number // Track bucket fullness
measurements: { // Embed up to 200 per bucket
timestamp: Date
value: number
}[]
}
```
## Indexing Strategy
```typescript
// Compound index: field order matters (ESR rule)
// Equality → Sort → Range
db.orders.createIndex({
status: 1, // Equality: exact match filter
createdAt: -1, // Sort: avoid in-memory sort
total: 1 // Range: price > 100
})
// Partial index: only index documents matching filter (smaller index)
db.orders.createIndex(
{ customerId: 1, createdAt: -1 },
{ partialFilterExpression: { status: 'pending' } }
)
// Text index for search
db.products.createIndex({ name: 'text', description: 'text' })
// TTL index for auto-expiration
db.sessions.createIndex(
{ createdAt: 1 },
{ expireAfterSeconds: 86400 } // Auto-delete after 24h
)
// Wildcard index for dynamic schemas
db.events.createIndex({ 'metadata.$**': 1 })
```
## Aggregation Pipeline
```typescript
// Sales analytics: top products by revenue per category
const pipeline = [
// Stage 1: Filter date range
{ $match: {
createdAt: { $gte: new Date('2025-01-01'), $lt: new Date('2025-02-01') },
status: 'paid'
}},
// Stage 2: Unwind embedded items array
{ $unwind: '$items' },
// Stage 3: Group by product
{ $group: {
_id: '$items.productId',
productName: { $first: '$items.name' },
totalRevenue: { $sum: { $multiply: ['$items.price', '$items.quantity'] } },
totalSold: { $sum: '$items.quantity' },
orderCount: { $addToSet: '$_id' }
}},
// Stage 4: Add computed fields
{ $addFields: {
orderCount: { $size: '$orderCount' },
avgOrderValue: { $divide: ['$totalRevenue', { $size: '$orderCount' }] }
}},
// Stage 5: Sort by revenue descending
{ $sort: { totalRevenue: -1 } },
// Stage 6: Limit to top 20
{ $limit: 20 },
// Stage 7: Lookup category details
{ $lookup: {
from: 'products',
localField: '_id',
foreignField: '_id',
pipeline: [{ $project: { categoryId: 1 } }],
as: 'product'
}}
]
const results = await db.orders.aggregate(pipeline).toArray()
```
## Change Streams (Real-time Reactivity)
```typescript
async function watchOrderChanges(): Promise<void> {
const pipeline = [
{ $match: {
operationType: { $in: ['insert', 'update'] },
'fullDocument.status': 'paid'
}}
]
// resumeAfter enables resuming from last processed change (crash recovery)
const changeStream = db.orders.watch(pipeline, {
fullDocument: 'updateLookup', // Include full document on updates
resumeAfter: await getLastResumeToken()
})
changeStream.on('change', async (event) => {
try {
await processOrderPayment(event.fullDocument!)
await saveResumeToken(event._id) // Persist for crash recovery
} catch (err) {
console.error('Change stream processing failed:', err)
}
})
changeStream.on('error', (err) => {
console.error('Change stream error:', err)
// Reconnect with resume token
setTimeout(() => watchOrderChanges(), 5000)
})
}
```
## Multi-Document Transactions
```typescript
async function transferFunds(
fromAccountId: string,
toAccountId: string,
amount: number
): Promise<void> {
const session = client.startSession()
try {
await session.withTransaction(async () => {
const from = await db.accounts.findOne(
{ _id: new ObjectId(fromAccountId) },
{ session }
)
if (!from || from.balance < amount) {
throw new Error('Insufficient funds')
}
await db.accounts.updateOne(
{ _id: new ObjectId(fromAccountId) },
{ $inc: { balance: -amount } },
{ session }
)
await db.accounts.updateOne(
{ _id: new ObjectId(toAccountId) },
{ $inc: { balance: amount } },
{ session }
)
await db.transactions.insertOne({
from: fromAccountId,
to: toAccountId,
amount,
createdAt: new Date()
}, { session })
})
} finally {
await session.endSession()
}
}
```
## Checklist
- [ ] Embed for 1:1 and 1:few; reference for 1:many and many:many
- [ ] Follow ESR (Equality-Sort-Range) for compound index field order
- [ ] Use partial indexes to reduce index size on filtered queries
- [ ] Set TTL indexes for session/temp data auto-cleanup
- [ ] Use aggregation pipeline for analytics (not client-side loops)
- [ ] Change streams with resume tokens for crash-safe event processing
- [ ] Keep documents under 16MB (MongoDB limit)
- [ ] Use `explain()` to verify queries use indexes
## Anti-Patterns
- Unbounded arrays: reviews/comments embedded in parent (grows forever, hits 16MB)
- Missing indexes: full collection scans on frequently queried fields
- $lookup in hot paths: use denormalization, not joins, for read-heavy queries
- Storing related data in separate collections when always read together
- Using MongoDB as a relational database (normalize everything)
- Not using write concern `majority` for critical writes (data loss risk)Related Skills
websocket-patterns
Connection management, room patterns, reconnection strategies, message buffering, and binary protocol design.
vector-db-patterns
Embedding strategies, ANN algorithms, hybrid search, RAG chunking strategies, and reranking for semantic search and retrieval.
tracing-patterns
OpenTelemetry setup, span context propagation, sampling strategies, Jaeger queries
terraform-patterns
Module composition, state management, workspace strategy, provider versioning, and infrastructure-as-code best practices.
swift-patterns
SwiftUI view composition, @Observable patterns, async/await concurrency, TCA architecture, and Combine reactive streams.
springboot-patterns
Spring Boot architecture patterns, REST API design, layered services, data access, caching, async processing, and logging. Use for Java Spring Boot backend work.
seo-patterns
Meta tag patterns, structured data (JSON-LD), Core Web Vitals optimization, and SSR/SSG strategies for search visibility.
secret-patterns
30+ service-specific secret detection regex patterns, entropy-based detection, PEM/JWT/Base64 identification, and false positive filtering.
saas-payment-patterns
Payment provider abstraction, webhook security, subscription lifecycle, dunning flows, pricing models, invoicing, tax handling, and refund patterns for SaaS applications.
saas-auth-patterns
SaaS authentication and authorization patterns including JWT vs session strategies, multi-tenant isolation, RBAC, API key management, passwordless flows, MFA, and secure session handling.
saas-analytics-patterns
SaaS analytics event taxonomy, metric formulas (MRR, churn, LTV), provider-agnostic tracking, funnel analysis, cohort setup, and privacy-respecting instrumentation.
revenuecat-patterns
RevenueCat SDK entegrasyon pattern'leri. iOS (Swift), Android (Kotlin), React Native ve Flutter icin setup, offerings, entitlement checking, webhook integration, StoreKit 2 migration ve sandbox testing.