mongodb-schema-design by romiluz13/mongodb-agent-skills
npx skills add https://github.com/romiluz13/mongodb-agent-skills --skill mongodb-schema-designMongoDB 的数据建模模式与反模式,由 MongoDB 维护。包含 5 个类别共 33 条规则,按影响程度排序。糟糕的模式是大多数 MongoDB 性能和成本问题的根源——查询和索引无法修复根本错误的模型。
在以下情况下参考这些指南:
| 优先级 | 类别 | 影响 | 前缀 | 规则数量 |
|---|---|---|---|---|
| 1 | 模式反模式 | 关键 | antipattern- | 7 |
| 2 | 模式基础 | 高 | fundamental- |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 5 |
| 3 | 关系模式 | 高 | relationship- | 6 |
| 4 | 设计模式 | 中 | pattern- | 12 |
| 5 | 模式验证 | 中 | validation- | 3 |
antipattern-unbounded-arrays - 绝不允许数组无限制增长antipattern-bloated-documents - 将文档保持在 16KB 以下以利于工作集antipattern-massive-arrays - 超过 1000 个元素的数组会损害性能antipattern-unnecessary-collections - 减少集合数量,增加嵌入antipattern-excessive-lookups - 通过反规范化减少 $lookupantipattern-schema-drift - 强制文档间结构一致性antipattern-unnecessary-indexes - 审计并移除未使用或冗余的索引fundamental-embed-vs-reference - 关系决策框架fundamental-data-together - 一起访问的数据存储在一起fundamental-document-model - 拥抱文档模型,避免 SQL 模式fundamental-schema-validation - 使用 JSON Schema 强制执行结构fundamental-16mb-awareness - 围绕 BSON 文档限制进行设计relationship-one-to-one - 为简单性嵌入,为独立性引用relationship-one-to-few - 嵌入有界数组(地址、电话号码)relationship-one-to-many - 为大型/无界关系使用引用relationship-one-to-squillions - 引用海量子集,存储摘要relationship-many-to-many - 双向引用以实现双向访问relationship-tree-structures - 父/子/物化路径模式pattern-archive - 将历史数据移至单独存储以提高性能pattern-attribute - 将多个可选字段折叠为键值属性pattern-bucket - 将时间序列或 IoT 数据分组到桶中pattern-time-series-collections - 在可用时使用原生时间序列集合pattern-extended-reference - 缓存频繁访问的相关数据pattern-subset - 在主文档中存储热数据,在其他地方存储冷数据pattern-computed - 预计算昂贵的聚合pattern-outlier - 处理具有异常数组大小的文档pattern-polymorphic - 使用类型鉴别器存储异构文档pattern-schema-versioning - 使用版本字段安全演进模式validation-json-schema - 在数据库级别验证数据类型和结构validation-action-levels - 为验证选择警告与错误模式validation-rollout-strategy - 在生产中安全引入验证"一起访问的数据应该存储在一起。"
这是 MongoDB 的核心理念。嵌入相关数据可以消除连接、减少往返次数并实现原子更新。仅在必要时才使用引用。
| 关系 | 基数 | 访问模式 | 建议 |
|---|---|---|---|
| 一对一 | 1:1 | 总是一起 | 嵌入 |
| 一对少 | 1:N (N < 100) | 通常一起 | 嵌入数组 |
| 一对多 | 1:N (N > 100) | 经常分开 | 引用 |
| 多对多 | M:N | 变化 | 双向引用 |
阅读单个规则文件以获取详细解释和代码示例:
rules/antipattern-unbounded-arrays.md
rules/relationship-one-to-many.md
rules/_sections.md
每个规则文件包含:
此技能中的每条规则都提供:
我可以分析代码模式,但如果没有数据库连接,我无法看到您的实际数据。这意味着我可能会建议:
在实施前务必验证。 每条规则都包含验证命令。
要进行自动验证,请连接 MongoDB MCP Server:
选项 1:连接字符串
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server", "--readOnly"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb+srv://user:pass@cluster.mongodb.net/mydb"
}
}
}
}
选项 2:本地 MongoDB
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server", "--readOnly"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb://localhost:27017/mydb"
}
}
}
}
⚠️ 安全:为安全起见使用 --readOnly。仅在需要写操作时移除。
连接后,我可以自动:
mcp__mongodb__collection-schema 推断模式mcp__mongodb__aggregate 测量文档/数组大小mcp__mongodb__db-stats 检查集合统计信息未经您明确批准,我绝不会执行写操作。
| 操作类型 | MCP 工具 | 操作 |
|---|---|---|
| 读取(安全) | find, aggregate, collection-schema, db-stats, count | 我可能会自动运行以进行验证 |
| 写入(需要批准) | update-many, insert-many, create-collection | 我将显示命令并等待您的"是" |
| 破坏性(需要批准) | delete-many, drop-collection, drop-database | 我将警告您并要求明确确认 |
当我建议模式更改或数据修改时:
您的数据库,您来决定。 我在这里提供建议,而不是单方面行动。
如果您不确定某个建议:
我们是一个团队——让我们一起把它做好。
包含所有规则扩展的完整指南:AGENTS.md
每周安装次数
189
仓库
GitHub 星标数
17
首次出现
2026年1月29日
安全审计
安装于
claude-code159
codex78
github-copilot75
opencode73
gemini-cli71
cursor71
Data modeling patterns and anti-patterns for MongoDB, maintained by MongoDB. Contains 33 rules across 5 categories , prioritized by impact. Bad schema is the root cause of most MongoDB performance and cost issues—queries and indexes cannot fix a fundamentally wrong model.
Reference these guidelines when:
| Priority | Category | Impact | Prefix | Rules |
|---|---|---|---|---|
| 1 | Schema Anti-Patterns | CRITICAL | antipattern- | 7 |
| 2 | Schema Fundamentals | HIGH | fundamental- | 5 |
| 3 | Relationship Patterns | HIGH | relationship- | 6 |
| 4 | Design Patterns | MEDIUM | pattern- | 12 |
| 5 | Schema Validation | MEDIUM | validation- | 3 |
antipattern-unbounded-arrays - Never allow arrays to grow without limitantipattern-bloated-documents - Keep documents under 16KB for working setantipattern-massive-arrays - Arrays over 1000 elements hurt performanceantipattern-unnecessary-collections - Fewer collections, more embeddingantipattern-excessive-lookups - Reduce $lookup by denormalizingantipattern-schema-drift - Enforce consistent structure across documentsantipattern-unnecessary-indexes - Audit and remove unused or redundant indexesfundamental-embed-vs-reference - Decision framework for relationshipsfundamental-data-together - Data accessed together stored togetherfundamental-document-model - Embrace documents, avoid SQL patternsfundamental-schema-validation - Enforce structure with JSON Schemafundamental-16mb-awareness - Design around BSON document limitrelationship-one-to-one - Embed for simplicity, reference for independencerelationship-one-to-few - Embed bounded arrays (addresses, phone numbers)relationship-one-to-many - Reference for large/unbounded relationshipsrelationship-one-to-squillions - Reference massive child sets, store summariesrelationship-many-to-many - Two-way referencing for bidirectional accessrelationship-tree-structures - Parent/child/materialized path patternspattern-archive - Move historical data to separate storage for performancepattern-attribute - Collapse many optional fields into key-value attributespattern-bucket - Group time-series or IoT data into bucketspattern-time-series-collections - Use native time series collections when availablepattern-extended-reference - Cache frequently-accessed related datapattern-subset - Store hot data in main doc, cold data elsewherepattern-computed - Pre-calculate expensive aggregationspattern-outlier - Handle documents with exceptional array sizespattern-polymorphic - Store heterogeneous documents with a type discriminatorvalidation-json-schema - Validate data types and structure at database levelvalidation-action-levels - Choose warn vs error mode for validationvalidation-rollout-strategy - Introduce validation safely in production"Data that is accessed together should be stored together."
This is MongoDB's core philosophy. Embedding related data eliminates joins, reduces round trips, and enables atomic updates. Reference only when you must.
| Relationship | Cardinality | Access Pattern | Recommendation |
|---|---|---|---|
| One-to-One | 1:1 | Always together | Embed |
| One-to-Few | 1:N (N < 100) | Usually together | Embed array |
| One-to-Many | 1:N (N > 100) | Often separate | Reference |
| Many-to-Many | M:N | Varies | Two-way reference |
Read individual rule files for detailed explanations and code examples:
rules/antipattern-unbounded-arrays.md
rules/relationship-one-to-many.md
rules/_sections.md
Each rule file contains:
Every rule in this skill provides:
I analyze code patterns, but I can't see your actual data without a database connection. This means I might suggest:
Always verify before implementing. Each rule includes verification commands.
For automatic verification, connect the MongoDB MCP Server:
Option 1: Connection String
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server", "--readOnly"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb+srv://user:pass@cluster.mongodb.net/mydb"
}
}
}
}
Option 2: Local MongoDB
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server", "--readOnly"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb://localhost:27017/mydb"
}
}
}
}
⚠️ Security : Use --readOnly for safety. Remove only if you need write operations.
When connected, I can automatically:
mcp__mongodb__collection-schemamcp__mongodb__aggregatemcp__mongodb__db-statsI will NEVER execute write operations without your explicit approval.
| Operation Type | MCP Tools | Action |
|---|---|---|
| Read (Safe) | find, aggregate, collection-schema, db-stats, count | I may run automatically to verify |
| Write (Requires Approval) | update-many, insert-many, create-collection |
When I recommend schema changes or data modifications:
Your database, your decision. I'm here to advise, not to act unilaterally.
If you're not sure about a recommendation:
We're a team—let's get this right together.
For the complete guide with all rules expanded: AGENTS.md
Weekly Installs
189
Repository
GitHub Stars
17
First Seen
Jan 29, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
claude-code159
codex78
github-copilot75
opencode73
gemini-cli71
cursor71
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
118,000 周安装
pattern-schema-versioning - Evolve schemas safely with version fields| I will show the command and wait for your "yes" |
| Destructive (Requires Approval) | delete-many, drop-collection, drop-database | I will warn you and require explicit confirmation |