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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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
schemaVersionfield to support future migrations.
Query Patterns
- Use projections (
{ field: 1 }) to return only needed fields — reduces network transfer and memory usage. - Use
$elemMatchfor querying and projecting specific array elements. - Use
$infor matching against a list of values. Use$existsand$typefor schema variations. - Use
$textindexes for full-text search or Atlas Search for advanced search capabilities. - Avoid
$whereand 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
$matchas early as possible in the pipeline to reduce the working set. - Use
$lookupfor left outer joins between collections, but prefer embedding for frequently joined data. - Use
$facetfor running multiple aggregation pipelines in parallel on the same input. - Use
$mergeor$outto 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()anddb.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
$regexwith 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
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。