{"skill":{"slug":"mongodb","displayName":"MongoDB","summary":"Design schemas, write queries, and configure MongoDB for consistency and performance.","description":"---\nname: MongoDB\nslug: mongodb\nversion: 1.0.1\ndescription: Design MongoDB schemas with proper embedding, indexing, aggregation, and production-ready patterns.\nmetadata: {\"clawdbot\":{\"emoji\":\"🍃\",\"requires\":{\"anyBins\":[\"mongosh\",\"mongo\"]},\"os\":[\"linux\",\"darwin\",\"win32\"]}}\n---\n\n## When to Use\n\nUser needs MongoDB expertise — from schema design to production optimization. Agent handles document modeling, indexing strategies, aggregation pipelines, consistency patterns, and scaling.\n\n## Quick Reference\n\n| Topic | File |\n|-------|------|\n| Schema design patterns | `schema.md` |\n| Index strategies | `indexes.md` |\n| Aggregation pipeline | `aggregation.md` |\n| Production configuration | `production.md` |\n\n## Schema Design Philosophy\n\n- Embed when data is queried together and doesn't grow unboundedly\n- Reference when data is large, accessed independently, or many-to-many\n- Denormalize for read performance, accept update complexity—no JOINs means duplicate data\n- Design for your queries, not for normalized elegance\n\n## Document Size Traps\n\n- 16MB max per document—plan for this from day one; use GridFS for large files\n- Arrays that grow infinitely = disaster—use bucketing pattern instead\n- BSON overhead: field names repeated per document—short names save space at scale\n- Nested depth limit 100 levels—rarely hit but exists\n\n## Array Traps\n\n- Arrays > 1000 elements hurt performance—pagination inside documents is hard\n- `$push` without `$slice` = unbounded growth; use `$push: {$each: [...], $slice: -100}`\n- Multikey indexes on arrays: index entry per element—can explode index size\n- Can't have multikey index on more than one array field in compound index\n\n## $lookup Traps\n\n- `$lookup` performance degrades with collection size—no index on foreign collection (until 5.0)\n- One `$lookup` per pipeline stage—nested lookups get complex and slow\n- `$lookup` with pipeline (5.0+) can filter before joining—massive improvement\n- Consider: if you $lookup frequently, maybe embed instead\n\n## Index Strategy\n\n- ESR rule: Equality fields first, Sort fields next, Range fields last\n- MongoDB doesn't do efficient index intersection—single compound index often better\n- Only one text index per collection—plan carefully; use Atlas Search for complex text\n- TTL index for auto-expiration: `{createdAt: 1}, {expireAfterSeconds: 86400}`\n\n## Consistency Traps\n\n- Default read/write concern not fully consistent—`{w: \"majority\", readConcern: \"majority\"}` for strong\n- Multi-document transactions since 4.0—but add latency and lock overhead; design to minimize\n- Single-document operations are atomic—exploit this by embedding related data\n- `retryWrites: true` in connection string—handles transient failures automatically\n\n## Read Preference Traps\n\n- Stale reads on secondaries—replication lag can be seconds\n- `nearest` for lowest latency—but may read stale data\n- Write always goes to primary—read preference doesn't affect writes\n- Read your own writes: use `primary` or session-based causal consistency\n\n## ObjectId Traps\n\n- Contains timestamp: `ObjectId.getTimestamp()`—extract creation time without extra field\n- Roughly time-ordered—can sort by `_id` for creation order without createdAt\n- Not random—predictable if you know creation time; don't rely on for security tokens\n\n## Performance Mindset\n\n- `explain(\"executionStats\")` shows actual execution—not just theoretical plan\n- `totalDocsExamined` vs `nReturned` ratio should be ~1—otherwise index missing\n- `COLLSCAN` in explain = full collection scan—add appropriate index\n- Covered queries: `IXSCAN` + `totalDocsExamined: 0`—all data from index\n\n## Aggregation Philosophy\n\n- Pipeline stages are transformations—think of data flowing through\n- Filter early (`$match`), project early (`$project`)—reduce data volume ASAP\n- `$match` at start can use indexes; `$match` after `$unwind` cannot\n- Test complex pipelines stage by stage—build incrementally\n\n## Common Mistakes\n\n- Treating MongoDB as \"schemaless\"—still need schema design; just enforced in app not DB\n- Not adding indexes—scans entire collection; every query pattern needs index\n- Giant documents via array pushes—hit 16MB limit or slow BSON parsing\n- Ignoring write concern—data may appear written but not persisted/replicated\n","topics":["MongoDB"],"tags":{"latest":"1.0.1"},"stats":{"comments":0,"downloads":3216,"installsAllTime":121,"installsCurrent":27,"stars":5,"versions":2},"createdAt":1770690113581,"updatedAt":1778486278647},"latestVersion":{"version":"1.0.1","createdAt":1771075841509,"changelog":"Added auxiliary files, Quick Reference, recovered ObjectId traps and Common Mistakes","license":null},"metadata":{"setup":[],"os":["linux","darwin","win32"],"systems":null},"owner":{"handle":"ivangdavila","userId":"s178jdk12x4qj3gs2se3etxf3h83h7ft","displayName":"Iván","image":"https://avatars.githubusercontent.com/u/81719670?v=4"},"moderation":null}