[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M79DWHZCFBGMWMED4NGVK0DM":3,"skill-same:01M79DWHZCFBGMWMED4NGVK0DM":48},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":29,"ownerHtmlUrl":30,"ownerBlogUrl":31,"ownerDescription":32,"updatedAt":33,"install":34,"resources":47},"01M79DWHZCFBGMWMED4NGVK0DM","mongodb-schema-and-indexing",null,"Production-grade MongoDB schema design patterns, indexing strategies, ESR rule (Equality, Sort, Range), aggregation framework optimization, and sharding\u002Fpartitioning architectures. Use when designing MongoDB schemas, tuning query performance, or building aggregation pipelines.","hamzabellouch\u002Fagent-skills","hamzabellouch","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F237856042?v=4","01M79BRYW7XAMK3YDWQEMTWGWS","Hamza Bellouch","01M79BRYWXQV3JK2TGVTX8GN2A",8,"MIT","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FProgramming\u002FBackend\u002FDatabases_and_Caching\u002Fmongodb-schema-and-indexing\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: mongodb-schema-and-indexing\ndescription: Production-grade MongoDB schema design patterns, indexing strategies, ESR rule (Equality, Sort, Range), aggregation framework optimization, and sharding\u002Fpartitioning architectures. Use when designing MongoDB schemas, tuning query performance, or building aggregation pipelines.\n---\n\n# MongoDB Schema Design & Indexing Optimization Guide\n\nComprehensive architectural reference for MongoDB schema modeling, high-performance indexing, aggregation pipeline tuning, and cluster sharding strategies.\n\n---\n\n## 1. Schema Modeling Framework: Embedding vs. Referencing\n\n### Decision Matrix\n\n```\n                      Do entities have a 1:1 or 1:Few relationship?\n                                     \u002F             \\\n                                  YES               NO\n                                  \u002F                   \\\n           Embed data inside parent document         Do child documents grow unbounded?\n                                                            \u002F             \\\n                                                         YES               NO\n                                                         \u002F                   \\\n                                            Use Two-Way Referencing      Embed with Subset Pattern\n```\n\n| Factor | Embedded Subdocuments | Referenced Documents (Normalized) |\n| :--- | :--- | :--- |\n| **Query Performance** | Fast ($O(1)$ single read, no joins) | Requires `$lookup` or application-side joins |\n| **Atomicity** | Atomic updates across whole document | Requires Multi-Document Transactions (`session`) |\n| **Document Size Limit** | Subject to 16 MB BSON hard limit | Multi-document architecture bypasses limit |\n| **Data Redundancy** | Possible duplication across documents | Single source of truth |\n\n---\n\n## 2. Advanced MongoDB Design Patterns\n\n### 2.1 Subset Pattern\n* **Problem**: Large embedded array (e.g., 5,000 product reviews) causes huge document size and slow memory loading.\n* **Solution**: Keep only the 10 most recent reviews in the main product document; store all historical reviews in a separate `reviews` collection.\n\n### 2.2 Extended Reference Pattern\n* **Problem**: Frequent joins (`$lookup`) just to fetch 1 or 2 fields (e.g., `customerName` and `customerEmail` on an `Order`).\n* **Solution**: Copy immutable or rarely changed target fields directly into the order document:\n  ```json\n  {\n    \"_id\": ObjectId(\"6697a4...\"),\n    \"totalAmount\": 149.99,\n    \"customer\": {\n      \"id\": ObjectId(\"6697a2...\"),\n      \"name\": \"Jane Doe\",\n      \"email\": \"jane@example.com\"\n    }\n  }\n  ```\n\n### 2.3 Bucket Pattern\n* **Problem**: Time-series or IoT metric data logging creates millions of tiny documents, inflating index overhead.\n* **Solution**: Group data points into hourly or daily bucket documents containing arrays of measurements:\n  ```json\n  {\n    \"sensorId\": \"TEMP-NODE-04\",\n    \"timestampDay\": ISODate(\"2026-07-18T00:00:00Z\"),\n    \"count\": 60,\n    \"measurements\": [\n      { \"sec\": 0, \"val\": 22.4 },\n      { \"sec\": 60, \"val\": 22.5 }\n    ]\n  }\n  ```\n\n### 2.4 Schema Versioning Pattern\nAdd a `schemaVersion` field to every document to allow zero-downtime lazy migration across schema iterations.\n\n---\n\n## 3. High-Performance Indexing & The ESR Rule\n\n### The ESR Rule (Equality, Sort, Range)\nWhen creating compound indexes, order fields according to:\n1. **E - Equality**: Fields matched on exact values first.\n2. **S - Sort**: Fields used for sorting results second.\n3. **R - Range**: Fields queried with inequality range operators (`$gte`, `$lte`, `$in`, `$ne`) last.\n\n```typescript\n\u002F\u002F Example: Querying active orders for a customer sorted by creation date within a price range\n\u002F\u002F Query: db.orders.find({ customerId: \"C123\", status: \"ACTIVE\", price: { $gte: 50 } }).sort({ createdAt: -1 })\n\n\u002F\u002F PERFECT INDEX according to ESR:\n\u002F\u002F Equality: customerId, status\n\u002F\u002F Sort:     createdAt\n\u002F\u002F Range:    price\n\ndb.orders.createIndex({\n  customerId: 1,\n  status: 1,\n  createdAt: -1,\n  price: 1\n});\n```\n\n### Specialized Index Types\n\n* **Partial Indexes**: Index only documents matching a specific expression (reduces index memory size):\n  ```typescript\n  db.users.createIndex(\n    { email: 1 },\n    { unique: true, partialFilterExpression: { email: { $type: \"string\" } } }\n  );\n  ```\n* **Compound Multikey Indexes**: Indexes over array fields. *Rule: A compound index can contain at most ONE array field.*\n* **TTL Indexes**: Automatically delete documents after a specified time interval:\n  ```typescript\n  db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 });\n  ```\n\n---\n\n## 4. Aggregation Framework Optimization\n\n1. **Early Filtering**: Always place `$match` and `$sort` stages at the very beginning of the pipeline to leverage indexes.\n2. **Projection Pushdown**: Use `$project` or `$unset` early to eliminate unused fields before memory-heavy operations (`$group`, `$lookup`).\n3. **Indexed Joins**: Ensure foreign collection fields targeted by `$lookup` have indexes:\n   ```javascript\n   \u002F\u002F Requires index on 'orders.userId'\n   db.users.aggregate([\n     { $match: { status: \"ACTIVE\" } },\n     {\n       $lookup: {\n         from: \"orders\",\n         localField: \"_id\",\n         foreignField: \"userId\",\n         as: \"userOrders\"\n       }\n     }\n   ]);\n   ```\n\n---\n\n## 5. Sharding & Partitioning Architecture\n\n### Shard Key Selection Principles\n* **High Cardinality**: Ensures distinct values split across thousands of chunks.\n* **Balanced Frequency**: Prevents a single value from creating a monolithic non-splitable chunk (Jumbo Chunk).\n* **Targeted Operations**: Choose keys that allow query routing directly to a single shard (avoiding **Scatter-Gather** queries across all shards).\n\n```\n          [ Query Router: mongos ]\n                 \u002F        \\\n   (Targeted Query)      (Scatter-Gather Query)\n               \u002F            \\\n   [ Shard A ]                [ Shard A ] + [ Shard B ] + [ Shard C ]\n```\n\n---\n\n## 6. Anti-Patterns & Common Pitfalls\n\n* ❌ **Unindexed Queries in Production**: Leads to COLLSCAN (collection scans) that exhaust RAM and CPU.\n* ❌ **Dynamic Field Names**: Storing keys as values (e.g., `{\"attr_color\": \"blue\"}` instead of `{\"name\": \"color\", \"value\": \"blue\"}`). Prevents effective indexing.\n* ❌ **Over-Indexing**: Every index adds latency to insert\u002Fupdate\u002Fdelete operations and consumes WiredTiger cache memory.\n* ❌ **Unbounded Array Growth**: Arrays growing beyond 1,000+ items cause document move overhead and memory pressure.\n","# MongoDB Schema Design & Indexing Optimization Guide\n\nComprehensive architectural reference for MongoDB schema modeling, high-performance indexing, aggregation pipeline tuning, and cluster sharding strategies.\n\n---\n\n## 1. Schema Modeling Framework: Embedding vs. Referencing\n\n### Decision Matrix\n\n```\n                      Do entities have a 1:1 or 1:Few relationship?\n                                     \u002F             \\\n                                  YES               NO\n                                  \u002F                   \\\n           Embed data inside parent document         Do child documents grow unbounded?\n                                                            \u002F             \\\n                                                         YES               NO\n                                                         \u002F                   \\\n                                            Use Two-Way Referencing      Embed with Subset Pattern\n```\n\n| Factor | Embedded Subdocuments | Referenced Documents (Normalized) |\n| :--- | :--- | :--- |\n| **Query Performance** | Fast ($O(1)$ single read, no joins) | Requires `$lookup` or application-side joins |\n| **Atomicity** | Atomic updates across whole document | Requires Multi-Document Transactions (`session`) |\n| **Document Size Limit** | Subject to 16 MB BSON hard limit | Multi-document architecture bypasses limit |\n| **Data Redundancy** | Possible duplication across documents | Single source of truth |\n\n---\n\n## 2. Advanced MongoDB Design Patterns\n\n### 2.1 Subset Pattern\n* **Problem**: Large embedded array (e.g., 5,000 product reviews) causes huge document size and slow memory loading.\n* **Solution**: Keep only the 10 most recent reviews in the main product document; store all historical reviews in a separate `reviews` collection.\n\n### 2.2 Extended Reference Pattern\n* **Problem**: Frequent joins (`$lookup`) just to fetch 1 or 2 fields (e.g., `customerName` and `customerEmail` on an `Order`).\n* **Solution**: Copy immutable or rarely changed target fields directly into the order document:\n  ```json\n  {\n    \"_id\": ObjectId(\"6697a4...\"),\n    \"totalAmount\": 149.99,\n    \"customer\": {\n      \"id\": ObjectId(\"6697a2...\"),\n      \"name\": \"Jane Doe\",\n      \"email\": \"jane@example.com\"\n    }\n  }\n  ```\n\n### 2.3 Bucket Pattern\n* **Problem**: Time-series or IoT metric data logging creates millions of tiny documents, inflating index overhead.\n* **Solution**: Group data points into hourly or daily bucket documents containing arrays of measurements:\n  ```json\n  {\n    \"sensorId\": \"TEMP-NODE-04\",\n    \"timestampDay\": ISODate(\"2026-07-18T00:00:00Z\"),\n    \"count\": 60,\n    \"measurements\": [\n      { \"sec\": 0, \"val\": 22.4 },\n      { \"sec\": 60, \"val\": 22.5 }\n    ]\n  }\n  ```\n\n### 2.4 Schema Versioning Pattern\nAdd a `schemaVersion` field to every document to allow zero-downtime lazy migration across schema iterations.\n\n---\n\n## 3. High-Performance Indexing & The ESR Rule\n\n### The ESR Rule (Equality, Sort, Range)\nWhen creating compound indexes, order fields according to:\n1. **E - Equality**: Fields matched on exact values first.\n2. **S - Sort**: Fields used for sorting results second.\n3. **R - Range**: Fields queried with inequality range operators (`$gte`, `$lte`, `$in`, `$ne`) last.\n\n```typescript\n\u002F\u002F Example: Querying active orders for a customer sorted by creation date within a price range\n\u002F\u002F Query: db.orders.find({ customerId: \"C123\", status: \"ACTIVE\", price: { $gte: 50 } }).sort({ createdAt: -1 })\n\n\u002F\u002F PERFECT INDEX according to ESR:\n\u002F\u002F Equality: customerId, status\n\u002F\u002F Sort:     createdAt\n\u002F\u002F Range:    price\n\ndb.orders.createIndex({\n  customerId: 1,\n  status: 1,\n  createdAt: -1,\n  price: 1\n});\n```\n\n### Specialized Index Types\n\n* **Partial Indexes**: Index only documents matching a specific expression (reduces index memory size):\n  ```typescript\n  db.users.createIndex(\n    { email: 1 },\n    { unique: true, partialFilterExpression: { email: { $type: \"string\" } } }\n  );\n  ```\n* **Compound Multikey Indexes**: Indexes over array fields. *Rule: A compound index can contain at most ONE array field.*\n* **TTL Indexes**: Automatically delete documents after a specified time interval:\n  ```typescript\n  db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 });\n  ```\n\n---\n\n## 4. Aggregation Framework Optimization\n\n1. **Early Filtering**: Always place `$match` and `$sort` stages at the very beginning of the pipeline to leverage indexes.\n2. **Projection Pushdown**: Use `$project` or `$unset` early to eliminate unused fields before memory-heavy operations (`$group`, `$lookup`).\n3. **Indexed Joins**: Ensure foreign collection fields targeted by `$lookup` have indexes:\n   ```javascript\n   \u002F\u002F Requires index on 'orders.userId'\n   db.users.aggregate([\n     { $match: { status: \"ACTIVE\" } },\n     {\n       $lookup: {\n         from: \"orders\",\n         localField: \"_id\",\n         foreignField: \"userId\",\n         as: \"userOrders\"\n       }\n     }\n   ]);\n   ```\n\n---\n\n## 5. Sharding & Partitioning Architecture\n\n### Shard Key Selection Principles\n* **High Cardinality**: Ensures distinct values split across thousands of chunks.\n* **Balanced Frequency**: Prevents a single value from creating a monolithic non-splitable chunk (Jumbo Chunk).\n* **Targeted Operations**: Choose keys that allow query routing directly to a single shard (avoiding **Scatter-Gather** queries across all shards).\n\n```\n          [ Query Router: mongos ]\n                 \u002F        \\\n   (Targeted Query)      (Scatter-Gather Query)\n               \u002F            \\\n   [ Shard A ]                [ Shard A ] + [ Shard B ] + [ Shard C ]\n```\n\n---\n\n## 6. Anti-Patterns & Common Pitfalls\n\n* ❌ **Unindexed Queries in Production**: Leads to COLLSCAN (collection scans) that exhaust RAM and CPU.\n* ❌ **Dynamic Field Names**: Storing keys as values (e.g., `{\"attr_color\": \"blue\"}` instead of `{\"name\": \"color\", \"value\": \"blue\"}`). Prevents effective indexing.\n* ❌ **Over-Indexing**: Every index adds latency to insert\u002Fupdate\u002Fdelete operations and consumes WiredTiger cache memory.\n* ❌ **Unbounded Array Growth**: Arrays growing beyond 1,000+ items cause document move overhead and memory pressure.\n",{"name":5,"description":7},"main","e905af21e5f1e1c3ca605e9656c3a7930360a84e","Programming\u002FBackend\u002FDatabases_and_Caching\u002Fmongodb-schema-and-indexing",1,"https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills","https:\u002F\u002Fhamzabellouch.github.io\u002F","https:\u002F\u002Fgithub.com\u002Fhamzabellouch","hamzabellouch.github.io","CEO & Founder of @tknoglobal | Researcher in Technology & Applied Sciences.","2026-10-17 03:20:19",{"source":8,"skillName":5,"commands":35,"prompt":45,"zipAvailable":46},[36,39,42],{"tool":37,"command":38},"npx","npx skills add hamzabellouch\u002Fagent-skills --skill mongodb-schema-and-indexing",{"tool":40,"command":41},"bunx","bunx skills add hamzabellouch\u002Fagent-skills --skill mongodb-schema-and-indexing",{"tool":43,"command":44},"pnpm","pnpm dlx skills add hamzabellouch\u002Fagent-skills --skill mongodb-schema-and-indexing","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FProgramming\u002FBackend\u002FDatabases_and_Caching\u002Fmongodb-schema-and-indexing\u002FSKILL.md",true,[],[49,56,63,70,77,84],{"skillNo":50,"name":51,"nameZh":6,"description":52,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":19},"01M79FHH2H815A9TAP6WNKH4Q8","security-audit","Comprehensive security guidance, source code audit, vulnerability assessment, and penetration test review for codebases, APIs, microservices, CLI tools, libraries, and LLM-backed applications. Triggers on requests to audit a codebase, conduct security reviews, find vulnerabilities or flaws, pen-test source code, or when the user asks in Arabic (فحص أمني، تدقيق الكود أمنياً، مراجعة الثغرات، فحص أمان المشروع). Supports Guidance mode (focused review) and Full Audit mode (deterministic 6-phase harness).","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FSecurity_Audit_and_Code_Review\u002Fsecurity-audit\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":59,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":60,"categories":61,"keywords":62,"publishedAt":19},"01M79FHBGXVBY6VQ1X7ETAZMF1","malware-analysis-reversing","Production malware analysis workflows, reverse engineering methodologies, isolated sandbox triage, static\u002Fdynamic binary analysis, memory forensics, and YARA detection rule authoring.","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FOffensive_Security_and_Pentesting\u002Fmalware-analysis-reversing\u002FSKILL.md",[],[],{"skillNo":64,"name":65,"nameZh":6,"description":66,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":67,"categories":68,"keywords":69,"publishedAt":19},"01M79FH6EACDR801TZCE4R8SAC","application-security-pentesting","Production Application Security (AppSec) standards, OWASP Top 10 vulnerability remediation, SAST\u002FDAST pipeline integration, secure code review, threat modeling, and defensive security posture.","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FOffensive_Security_and_Pentesting\u002Fapplication-security-pentesting\u002FSKILL.md",[],[],{"skillNo":71,"name":72,"nameZh":6,"description":73,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":74,"categories":75,"keywords":76,"publishedAt":19},"01M79FH0VKVT44FK923K520ZQA","oauth2-oidc-security-flows","Implement enterprise OAuth 2.1 and OpenID Connect (OIDC) authorization code flow with Proof Key for Code Exchange (PKCE), state and nonce verification, refresh token rotation, token revocation, and centralized IdP federation (Keycloak, Auth0, Okta). Trigger when building secure authentication, single sign-on (SSO), or third-party delegated authorization.","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FIdentity_and_Access_Management_IAM\u002Foauth2-oidc-security-flows\u002FSKILL.md",[],[],{"skillNo":78,"name":79,"nameZh":6,"description":80,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":81,"categories":82,"keywords":83,"publishedAt":19},"01M79FGVVB3R4T88XXD8FRSV5B","jwt-session-management-hardening","Harden JWT-based authentication and session lifecycles using asymmetric signing (RS256\u002FEdDSA), JWKS rotation, strict claims validation, Redis-backed sliding window sessions, revocation lists, and secure HttpOnly cookie storage. Trigger when implementing token validation, session revocation, or hardening API authentication.","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FIdentity_and_Access_Management_IAM\u002Fjwt-session-management-hardening\u002FSKILL.md",[],[],{"skillNo":85,"name":86,"nameZh":6,"description":87,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":88,"categories":89,"keywords":90,"publishedAt":19},"01M79FGNCPZ85CVEY0B3NE4H3B","soc2-compliance-automation","Best practices for implementing SOC 2 Type II compliance controls, continuous evidence collection, infrastructure policy-as-code, and automated security monitoring. Use when automating Trust Services Criteria (Security, Availability, Confidentiality, Processing Integrity, Privacy), audit logging, IAM least privilege, or CI\u002FCD security gates.","https:\u002F\u002Fgithub.com\u002Fhamzabellouch\u002Fagent-skills\u002Fblob\u002Fmain\u002FSecurity\u002FGovernance_Compliance_and_Legal_Tech\u002Fsoc2-compliance-automation\u002FSKILL.md",[],[]]