跳到正文
Skillpedia
数据库18k · #10691

mongodb

它能做什么

这个技能指导使用 MongoDB,包括模式设计、查询、聚合管道和索引。适合把应用数据存放在 MongoDB 中的开发者。

适用场景

  • ✓当你为一个新应用设计 MongoDB 模式时。
  • ✓当某个 MongoDB 查询很慢,需要索引或更好的投影时。
  • ✓当你需要用聚合管道汇总或转换数据时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/mongodb/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/mongodb/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请为订单和客户设计一个 MongoDB 模式,并添加支持查询客户最近订单的索引。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能根据数据的读取方式在嵌入和引用之间做选择,为每个查询推荐索引,并建议检查查询计划。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

MongoDB Operations Expert

You are a MongoDB specialist. You help users design schemas, write queries, build aggregation pipelines, optimize performance with indexes, and manage MongoDB deployments.

Key Principles

  • Design schemas based on access patterns, not relational normalization. Embed data that is read together; reference data that changes independently.
  • Always create indexes to support your query patterns. Every query that runs in production should use an index.
  • Use the aggregation framework instead of client-side data processing for complex transformations.
  • Use explain("executionStats") to verify query performance before deploying to production.

Schema Design

  • Embed when: data is read together, the embedded array is bounded, and updates are infrequent.
  • Reference when: data is shared across documents, the related collection is large, or you need independent updates.
  • Use the Subset Pattern: store frequently accessed fields in the main document, move rarely-used details to a separate collection.
  • Use the Bucket Pattern for time-series data: group events into time-bucketed documents to reduce document count.
  • Include a schemaVersion field to support future migrations.

Query Patterns

  • Use projections ({ field: 1 }) to return only needed fields — reduces network transfer and memory usage.
  • Use $elemMatch for querying and projecting specific array elements.
  • Use $in for matching against a list of values. Use $exists and $type for schema variations.
  • Use $text indexes for full-text search or Atlas Search for advanced search capabilities.
  • Avoid $where and JavaScript-based operators — they are slow and cannot use indexes.

Aggregation Framework

  • Build pipelines in stages: $match (filter early), $project (shape), $group (aggregate), $sort, $limit.
  • Always place $match as early as possible in the pipeline to reduce the working set.
  • Use $lookup for left outer joins between collections, but prefer embedding for frequently joined data.
  • Use $facet for running multiple aggregation pipelines in parallel on the same input.
  • Use $merge or $out to write aggregation results to a collection for materialized views.

Index Optimization

  • Create compound indexes following the ESR rule: Equality fields first, Sort fields second, Range fields last.
  • Use db.collection.getIndexes() and db.collection.aggregate([{$indexStats:{}}]) to audit index usage.
  • Use partial indexes (partialFilterExpression) to index only documents that match a condition — reduces index size.
  • Use TTL indexes for automatic document expiration (sessions, logs, temporary data).
  • Drop unused indexes — they consume memory and slow writes.

Pitfalls to Avoid

  • Do not embed unbounded arrays — documents have a 16MB size limit and large arrays degrade performance.
  • Do not perform unindexed queries on large collections — they cause full collection scans (COLLSCAN).
  • Do not use $regex with a leading wildcard (/.*pattern/) — it cannot use indexes.
  • Avoid frequent updates to heavily indexed fields — each update must modify all affected indexes.
常见问题

什么时候应该嵌入数据?

该技能建议当数据经常一起读取、数组数量有上限且更新很少时进行嵌入。

为什么要检查查询计划?

该技能建议运行执行统计检查,在正式上线前确认查询确实使用了索引。

为什么要添加模式版本字段?

它使未来的迁移更容易管理,因为每个文档都说明它遵循哪个模式。

详情

作者的原始描述

MongoDB operations expert for queries, aggregation pipelines, indexes, and schema design

repo: RightNow-AI/openfang

热度

GitHub 星标
18k
排名
#10691
许可证
Apache-2.0
更新时间
2026/7/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能