fcakyon/claude-codex-settings / plugins/mongodb-skills/skills/mongodb-schema-design
mongodb-schema-design
VerifiedMongoDB schema design patterns and anti-patterns. Use when designing data models, reviewing schemas, migrating from SQL, or troubleshooting performance issues caused by schema problems. Triggers on "design schema", "embed vs reference", "MongoDB data model", "schema review", "unbounded arrays", "one-to-many", "tree structure", "16MB limit", "schema validation", "JSON Schema", "time series", "schema migration", "polymorphic", "TTL", "data lifecycle", "archive", "index explosion", "unnecessary indexes", "approximation pattern", "document versioning".
references/source-query-stats.md
View on GitHub# Query Stats
## When to use
Analyzes query access patterns with minimal performance overhead. Use for identifying co-accessed fields, collection relationships, and query frequencies. Only supports `find`, `aggregate`, and `distinct` operations.
## Requirements
Atlas M10+ tier.
## How to use
Aggregate on the admin database.
With mcp-server, use the `mcp__mongodb__aggregateDB` tool with database set to `admin`.
```javascript
db.getSiblingDB("admin").aggregate([{ $queryStats: {} }])
```
**Example 1: Find collections frequently queried together with others (embedding candidates)**
```javascript
db.aggregate([
{ $queryStats: {} },
{
$match: {
"key.queryShape.cmdNs.db": "databaseName",
"key.queryShape.command": "aggregate",
"key.queryShape.pipeline.$lookup": { $exists: true }
}
},
{ $unwind: "$key.queryShape.pipeline" },
{
$match: { "key.queryShape.pipeline.$lookup": { $exists: true } }
},
{
$set: {
stageKeyValue: {
$first: { $objectToArray: "$key.queryShape.pipeline" }
}
}
},
{
$group: {
_id: {
source: "$key.queryShape.cmdNs.coll",
target: "$stageKeyValue.v.from"
},
totalLookupHits: { $sum: "$metrics.execCount" },
avgPipelineMs: {
$avg: { $divide: [
{ $divide: ["$metrics.totalExecMicros.sum", 1000] },
"$metrics.execCount"
]}
}
}
},
{ $sort: { totalLookupHits: -1 } }
])
// High totalLookupHits = frequently joined
// High avgPipelineMS = lookup is part of slow queries (does not automatically mean that the $lookup is slow, could be the whole pipeline - see the full query shapes)
// High scores on both - consider embedding to avoid $lookup
```
**Example 1.1: Find query shapes that use $lookup on specific collections**
```javascript
db.aggregate([
{ $queryStats: {} },
{
$match: {
"key.queryShape.cmdNs.db": "databaseName",
"key.queryShape.command": "aggregate",
"key.queryShape.cmdNs.coll": "sourceCollectionName",
"key.queryShape.pipeline.$lookup.from": "targetCollectionName"
}
},
{
$project: {
database: "$key.queryShape.cmdNs.db",
collection: "$key.queryShape.cmdNs.coll",
pipeline: "$key.queryShape.pipeline",
execCount: "$metrics.execCount",
avgMs: {
$divide: [
{ $divide: ["$metrics.totalExecMicros.sum", 1000] },
"$metrics.execCount"
]
}
},
},
{ $sort: { execCount: -1 } },
{ $limit: 10 }
])
```
**Example 2: Find top most frequent query shapes (optimize hot paths)**
```javascript
db.getSiblingDB("admin").aggregate([
{ $queryStats: {} },
{ $sort: { "metrics.execCount": -1 } },
{ $limit: 10 },
{
$project: {
command: "$key.queryShape.command",
database: "$key.queryShape.cmdNs.db",
collection: "$key.queryShape.cmdNs.coll",
queryShape: "$key.queryShape",
execCount: "$metrics.execCount",
avgMs: {
$divide: [
{ $divide: ["$metrics.totalExecMicros.sum", 1000] },
"$metrics.execCount"
]
}
}
}
])
// High execCount = hot path → design your schema for these queries first
// Cross reference with avgMS or [slow query logs](references/source-slow-query-logs.md) to find queries that are both frequent and slow
// Note: Query stats do not include write patterns (update, insert)
```Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |