前言
软件工程正在经历一场深刻的变革。智能体工程(Agent Engineering) 时代已经到来。
根据 Stack Overflow 2025 年开发者调查显示:
- 84% 的受访者已在开发中使用或计划使用 AI 工具
- 这一比例高于 2024 年的 76%
在这个背景下,MongoDB 在去年推出 MongoDB MCP 服务器 之后,现在又正式发布了官方 MongoDB Agent Skills(代理技能)。这标志着数据库厂商开始主动拥抱 AI 辅助开发,并为智能体提供结构化的专家指导。
作为开发者,我们需要理解这一趋势,并学会如何利用这些工具提升开发效率。
一、背景:为什么需要 Agent Skills?
1.1 MongoDB MCP 服务器的局限性
去年 MongoDB 推出的 MCP(Model Context Protocol)服务器解决了连接问题:
# MongoDB MCP 服务器提供了连接能力
npx @mongodb/mcp-server@latest setup
但是,连接只是第一步。
智能体本质上是通用的,它们并不了解真实生产系统中所需的最佳实践和设计模式。这就像一个刚毕业的大学生,虽然会用编程语言,但缺乏实际项目经验。
1.2 编码智能体的常见问题
在实际使用中,编码智能体(如 Claude Code、Cursor、GitHub Copilot 等)经常会犯一些典型错误:
问题 1:过度规范化数据模式
bash
// ❌ 智能体常犯的错误:沿用关系型数据库思维
// 将用户信息分散到多个集合
const user = {
_id: ObjectId("..."),
username: "alice",
email: "alice@example.com"
};
const userProfile = {
userId: ObjectId("..."), // 外键关联
bio: "...",
avatar: "..."
};
const userSettings = {
userId: ObjectId("..."), // 另一个外键
theme: "dark",
notifications: true
};
// ✅ MongoDB 的最佳实践:嵌入相关数据
const user = {
_id: ObjectId("..."),
username: "alice",
email: "alice@example.com",
profile: {
bio: "...",
avatar: "..."
},
settings: {
theme: "dark",
notifications: true
}
};
问题 2:复合索引使用不足
// ❌ 智能体常犯的错误:创建多个单字段索引
db.orders.createIndex({ userId: 1 });
db.orders.createIndex({ status: 1 });
db.orders.createIndex({ createdAt: -1 });
// 查询时无法高效利用索引
db.orders.find({
userId: "123",
status: "pending"
}).sort({ createdAt: -1 });
// ✅ MongoDB 的最佳实践:使用复合索引
db.orders.createIndex({
userId: 1,
status: 1,
createdAt: -1
});
问题 3:索引和搜索索引滥用
// ❌ 智能体常犯的错误:滥用全文搜索索引
// 为简单查询创建 Atlas Search 索引
{
"mappings": {
"dynamic": true // 为所有字段创建搜索索引
}
}
// ✅ MongoDB 的最佳实践:根据需求选择合适的索引类型
// 精确匹配使用普通索引
db.products.createIndex({ sku: 1 }, { unique: true });
// 文本搜索才使用 Atlas Search
// 并且明确指定需要搜索的字段
二、MongoDB Agent Skills:核心发布
2.1 什么是 Agent Skills?
MongoDB Agent Skills 是一套结构化的说明、最佳实践和资源,智能体可以主动发现并应用这些内容,从而在整个开发生命周期中生成更可靠的代码。
# Agent Skill 的开放式标准结构示例
name: mongodb-schema-design
description: MongoDB 模式设计最佳实践
version: 1.0.0
instructions: |
1. 优先使用嵌入式数据模型
2. 避免过度的规范化
3. 考虑查询模式和访问模式
heuristics:
- rule: "一对一关系应嵌入"
example: "用户和用户资料"
- rule: "一对多关系视情况嵌入或引用"
example: "订单和订单项可嵌入"
anti-patterns:
- pattern: "将 MongoDB 当作关系型数据库使用"
problem: "频繁的 $lookup 操作导致性能问题"
2.2 Agent Skills 的覆盖范围
MongoDB 的初始版本覆盖了应用开发的完整生命周期:
| 领域 | 内容 | 示例 |
|---|---|---|
| 模式设计 | Schema Design Heuristics | 嵌入式 vs 引用式、原子性操作 |
| 性能优化 | Index Strategies | 复合索引、覆盖查询、分片策略 |
| AI 检索 | Advanced Features | Vector Search、Atlas Search |
| 连接管理 | Connection Management | 连接池配置、重试逻辑 |
2.3 Agent Skills 是开放标准
- Agent Skills 由 Anthropic 作为开放标准推出
- 目前已被 Claude Code、Cursor、Codex 等主流 AI 开发工具广泛采用
- 这意味着您可以在多个工具中复用同一套技能
三、MongoDB 插件:无缝集成到开发工具
3.1 为什么需要插件?
虽然 Agent Skills 提供了知识,但智能体需要主动发现并应用 这些技能。为了简化这一过程,MongoDB 为常用工具提供了插件 ,将 MCP 服务器和 Agent Skills 整合为开箱即用的软件包。
3.2 支持的工具
| 工具 | 类型 | 安装方式 |
|---|---|---|
| Claude Code | AI 编码助手 | /plugin install mongodb |
| Cursor | AI 代码编辑器 | 通过 Cursor 插件市场安装 |
| Gemini CLI | Google 命令行 AI | 通过 Gemini 插件系统安装 |
| VS Code | 微软代码编辑器 | 通过 VS Code 扩展市场安装 |
3.3 插件的工作流程
四、MCP 服务器 + Agent Skills:协同工作
4.1 MongoDB MCP 服务器的功能
作为 MongoDB 部署的连接层,MCP 服务器负责:
// MCP 服务器的配置示例
{
"mongodb": {
"command": "npx",
"args": ["@mongodb/mcp-server@latest"],
"env": {
"MONGODB_URI": "mongodb://localhost:27017/myapp",
"MONGODB_DB": "myapp"
}
}
}
核心能力:
-
管理认证
- 安全存储连接字符串
- 管理凭证和权限
-
精确控制智能体操作
- 定义智能体可以访问的集合
- 限制可以执行的命令(如禁止
dropDatabase)
-
与 MongoDB 原生授权机制集成
- 利用 MongoDB 的角色和权限系统
- 确保智能体仅拥有必要权限(最小权限原则)
-
可配置的控制措施
// 禁用危险操作 { "disabledTools": [ "dropCollection", "dropDatabase", "shutdown" ] }
4.2 Agent Skills 的价值
| 价值 | 说明 | 示例 |
|---|---|---|
| 确保遵循最佳实践 | 从一开始就将专家经验注入代码生成 | 自动建议复合索引 |
| 降低架构风险 | 避免常见的设计错误 | 防止过度规范化 |
| 加速开发效率 | 减少反复修改和重构的时间 | 一次性生成优化后的模式 |
| 提升代码质量 | 每个智能体生成的代码都基于统一标准 | 团队所有成员获得一致的代码质量 |
五、实战:如何开始使用?
5.1 在 Claude Code 中安装
第一步:安装 MongoDB 插件(包含 MCP 服务器和 Agent Skills)
# 在 Claude Code 中执行
/plugin install mongodb
第二步:配置 MongoDB 连接
# 设置环境变量
export MONGODB_URI="mongodb://localhost:27017/myapp"
export MONGODB_DB="myapp"
# 或者使用 .env 文件
echo "MONGODB_URI=mongodb://localhost:27017/myapp" > .env
echo "MONGODB_DB=myapp" >> .env
第三步:开始使用
现在,您可以直接向 Claude Code 提出 MongoDB 相关的问题:
💬 示例对话:
用户:帮我设计一个电商平台的订单系统数据模型
Claude:根据 MongoDB 最佳实践,我建议采用以下嵌入式数据模型:
1. 订单主文档(包含订单项嵌入)
2. 使用复合索引优化查询
3. 考虑分片策略以支持水平扩展
[生成优化后的代码...]
5.2 使用 Vercel Skills CLI 安装技能
如果您使用的工具不在官方插件支持列表中,可以手动安装 Agent Skills:
# 安装 Vercel Skills CLI(需要 Node.js)
npx skills add mongodb/agent-skills
手动安装方式:
# 1. 克隆 MongoDB Agent Skills 仓库
git clone https://github.com/mongodb/agent-skills.git
# 2. 将技能复制到您的项目目录
cp -r agent-skills/skills/* ~/.config/your-ai-tool/skills/
# 3. 重启 AI 工具以加载技能
5.3 安装独立的 MongoDB MCP 服务器
如果您只需要连接能力,而不需要 Agent Skills:
# 安装并配置 MongoDB MCP 服务器
npx @mongodb/mcp-server@latest setup
配置示例(For Claude Code):
// ~/.config/claude/mcp.json
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["@mongodb/mcp-server@latest"],
"env": {
"MONGODB_URI": "mongodb://localhost:27017",
"MONGODB_DATABASE": "myapp"
}
}
}
}
六、实际应用案例
6.1 案例 1:优化现有的数据模型
场景: 您有一个使用 MongoDB 的电商应用,但性能不佳。
使用 MongoDB Agent Skills 之前:
bash
// 智能体生成的代码(缺乏最佳实践指导)
const Product = {
_id: ObjectId("..."),
name: "iPhone 15",
price: 999
};
const Inventory = {
_id: ObjectId("..."),
productId: ObjectId("..."), // 外键
quantity: 50
};
const Category = {
_id: ObjectId("..."),
name: "Electronics"
};
const ProductCategory = {
_id: ObjectId("..."),
productId: ObjectId("..."), // 外键
categoryId: ObjectId("...") // 外键
};
// 查询需要多次 $lookup
db.products.aggregate([
{
$lookup: {
from: "inventories",
localField: "_id",
foreignField: "productId",
as: "inventory"
}
},
{
$lookup: {
from: "productcategories",
localField: "_id",
foreignField: "productId",
as: "categories"
}
}
]);
使用 MongoDB Agent Skills 之后:
// 智能体生成的代码(应用了最佳实践)
const Product = {
_id: ObjectId("..."),
name: "iPhone 15",
price: 999,
quantity: 50, // 嵌入库存信息
categories: ["Electronics", "Smartphones"], // 嵌入分类
// 使用复合索引
// db.products.createIndex({ name: 1, price: 1 })
};
// 查询简单且高效
db.products.find({
name: /iPhone/,
price: { $lt: 1000 }
});
性能对比:
| 指标 | 优化前 | 优化后 | 提升 |
|---|---|---|---|
| 查询响应时间 | 250ms | 15ms | 94% |
| 需要的索引数量 | 5个 | 2个 | 60% |
| 代码复杂度 | 高(多个集合) | 低(单个集合) | 显著改善 |
6.2 案例 2:为 AI 应用添加向量搜索
场景: 您正在构建一个 RAG(检索增强生成)应用,需要存储和检索文档的向量嵌入。
使用 Agent Skills 生成代码:
bash
// 智能体会自动应用 MongoDB Vector Search 的最佳实践
// 1. 创建包含向量字段的集合
db.documents.createCollection("documents");
// 2. 创建向量搜索索引
db.documents.createSearchIndex(
"vector_index",
{
"mappings": {
"dynamic": false,
"fields": {
"embedding": {
"dimensions": 1536,
"similarity": "cosine",
"type": "knnVector"
},
"content": {
"type": "string"
}
}
}
}
);
// 3. 插入文档(包含向量嵌入)
db.documents.insertMany([
{
content: "MongoDB 是一个文档数据库",
embedding: [0.1, 0.2, ..., 0.5] // 1536 维向量
},
{
content: "Agent Skills 提供最佳实践",
embedding: [0.2, 0.3, ..., 0.6]
}
]);
// 4. 执行向量搜索
db.documents.aggregate([
{
$vectorSearch: {
"index": "vector_index",
"path": "embedding",
"queryVector": [0.15, 0.25, ..., 0.55],
"numCandidates": 100,
"limit": 5
}
}
]);
Agent Skills 提供的额外指导:
# Agent Skills 会自动提醒以下最佳实践:
提醒 1: "确保 embedding 字段的维度与您的 AI 模型输出匹配"
- OpenAI text-embedding-3-small: 1536 维
- OpenAI text-embedding-3-large: 3072 维
提醒 2: "对于大规模向量搜索,调整 numCandidates 参数以平衡性能和准确性"
- 较小数据集: numCandidates = limit * 10
- 较大数据集: numCandidates = limit * 50
提醒 3: "考虑使用分区集合来优化大规模向量存储的性能"
七、组织知识沉淀:自定义 Agent Skills
7.1 为什么需要自定义技能?
虽然 MongoDB 官方提供了通用的最佳实践,但每个组织都有自己的:
- 内部命名规范(如集合名称前缀规则)
- 自定义数据建模模式(如多租户 SaaS 应用的数据隔离策略)
- 团队特定的工作流程(如代码审查检查清单)
Agent Skills 提供了一种切实可行的方法,将组织知识系统化。
7.2 创建自定义 Skill
示例:为团队创建命名规范 Skill
bash
# ~/.config/your-ai-tool/skills/mongodb-naming-conventions.yaml
name: mongodb-naming-conventions
description: 团队 MongoDB 命名规范
version: 1.0.0
author: "Your Team"
instructions: |
本团队 MongoDB 命名规范:
1. 集合命名:
- 使用小写字母和下划线
- 使用复数形式(如:users, order_items)
- 多租户集合添加租户前缀(如:tenant1_users)
2. 字段命名:
- 使用 camelCase(如:createdAt, userId)
- 避免使用 MongoDB 保留字(如:id, db)
3. 索引命名:
- 使用字段名 + 索引类型(如:idx_userId, idx_createdAt_desc)
examples:
- input: "创建一个用户集合"
output: |
db.createCollection("users");
// 创建常用索引
db.users.createIndex({ userId: 1 }, { name: "idx_userId" });
db.users.createIndex({ createdAt: -1 }, { name: "idx_createdAt_desc" });
7.3 在团队中共享 Skills
# 1. 将自定义 Skills 提交到团队的 Git 仓库
git add skills/
git commit -m "Add team MongoDB naming conventions skill"
git push origin main
# 2. 其他团队成员安装
git pull origin main
cp -r skills/* ~/.config/your-ai-tool/skills/
八、安全与治理
8.1 MCP 服务器的安全控制
MongoDB MCP 服务器提供了多层安全控制:
// MCP 服务器配置:安全加固示例
{
"mongodb": {
"command": "npx",
"args": ["@mongodb/mcp-server@latest"],
"env": {
"MONGODB_URI": "mongodb://readonly-user:password@localhost:27017/myapp",
"MONGODB_READONLY": "true"
},
"disabledTools": [
"dropCollection",
"dropDatabase",
"insert",
"update",
"delete"
]
}
}
最佳实践:
-
使用只读账户进行开发
- 防止智能体意外修改或删除数据
-
在生产环境中禁用危险操作
- 通过
disabledTools列表限制智能体能力
- 通过
-
审计智能体的所有操作
// 启用 MongoDB 审计日志 db.adminCommand({ setParameter: 1, auditAuthorizationSuccess: true });
8.2 Agent Skills 的治理
| 治理维度 | 措施 | 工具 |
|---|---|---|
| 版本控制 | 将 Skills 存储在 Git 仓库中 | Git + GitHub/GitLab |
| 代码审查 | 对 Skill 的修改进行同行评审 | Pull Request |
| 测试验证 | 为 Skill 编写测试用例 | 自动化测试 |
| 访问控制 | 限制谁可以修改 Skills | 仓库权限设置 |
九、未来展望
9.1 MongoDB 的持续投入
MongoDB 表示将根据用户反馈持续更新和扩展技能库,可能覆盖更多领域:
- 迁移指南:从关系型数据库迁移到 MongoDB
- 性能调优:高级性能分析和优化建议
- 安全加固:MongoDB 安全配置最佳实践
- 多云部署:MongoDB Atlas 在多云环境中的部署策略
9.2 对行业的启示
MongoDB 的这一举措为整个数据库行业树立了榜样:
# 未来我们可能会看到:
npx @postgres/mcp-server@latest setup
npx @redis/mcp-server@latest setup
npx @elastic/mcp-server@latest setup
数据库厂商应该:
- 提供官方 MCP 服务器,简化连接配置
- 提供官方 Agent Skills,传授最佳实践
- 与主流 AI 工具集成,提供开箱即用的插件
十、总结与行动建议
10.1 核心要点
| 要点 | 说明 |
|---|---|
| Agent Skills = 专家经验 | 将 MongoDB 专家的最佳实践系统化,让智能体能够应用这些知识 |
| MCP Server = 连接能力 | 提供安全、可控的数据库连接能力 |
| 插件 = 无缝集成 | 将两者整合,开箱即用 |
| 开放标准 | Agent Skills 是 Anthropic 推出的开放标准,可被多种工具采用 |
10.2 立即行动
对于开发者:
# 1. 安装 MongoDB 插件(以 Claude Code 为例)
/plugin install mongodb
# 2. 配置您的 MongoDB 连接
export MONGODB_URI="your_connection_string"
# 3. 开始体验智能体辅助的 MongoDB 开发
对于团队负责人:
- 评估您的团队是否可以受益于 AI 辅助开发
- 制定 AI 工具的使用规范和安全策略
- 创建自定义的 Agent Skills,沉淀团队最佳实践
对于数据库厂商:
- 学习 MongoDB 的做法,为您的数据库产品提供类似的 AI 辅助开发支持
参考资源
- MongoDB 官方博客:(原文链接)
- MongoDB MCP 服务器 :https://github.com/mongodb/mcp-server
- Anthropic Agent Skills 规范 :https://github.com/anthropics/agent-skills
- Vercel Skills CLI :https://github.com/vercel-labs/skills
- Stack Overflow 2025 开发者调查 :https://survey.stackoverflow.co/2025/
附录:常用命令速查表
bash
# ─── 安装 ─────────────────────────────────────────────
# Claude Code
/plugin install mongodb
# 使用 Vercel Skills CLI
npx skills add mongodb/agent-skills
# 安装独立的 MCP 服务器
npx @mongodb/mcp-server@latest setup
# ─── 配置 ─────────────────────────────────────────────
# 设置环境变量
export MONGODB_URI="mongodb://localhost:27017/myapp"
export MONGODB_DB="myapp"
# ─── 验证 ─────────────────────────────────────────────
# 测试连接
mongosh "$MONGODB_URI" --eval "db.runCommand({ connectionStatus: 1 })"
# 检查 MCP 服务器状态(在 AI 工具中)
/status mongodb