对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB MCP 服务器使用示例

本页通过带有自然语言提示的实际示例演示了如何使用MongoDB MCP 服务器。每个示例都显示了您可以在AI客户端中输入的提示,以及您可能从大型语言模型 (LLM) 收到的示例响应。

您可以使用此页面上的示例作为起点,与MongoDB MCP 服务器进行交互。

注意

您收到的确切输出将根据您的数据、AI客户端和您使用的LLM而有所不同。在这些示例中,组织ID、项目ID 和密码等私有信息都经过屏蔽。

在运行这些示例之前,请确保您具备以下条件:

以下示例演示了如何使用MongoDB MCP 服务器管理Atlas基础架构。

概述您的Atlas帐户结构和可用资源。

Show my Atlas organizations and projects

在单个工作流程中设置一个具有集群和所有必要访问权限配置的新Atlas项目。

Create a new Atlas project named myNewProject and create a
free cluster named myNewCluster in the new project, add
access list for my current IP, create a database user named
myNewUser with read and write access to the new cluster, and
return the connection string for the new cluster

要查找Atlas支持的云提供商的地区代码和人类可读位置,请使用 atlas-get-regions。这对于解析自然语言位置非常有用,例如“Iowa”(爱荷华州)。或“北欧”,转换为 atlas-create-cluster 和 atlas-upgrade-cluster 等工具所期望的确切地区代码。

What Atlas regions are available on GCP?

使用 atlas-create-cluster 创建具有 M10–M80 实例大小的专用 Atlas 集群。要创建免费套餐集群,请使用 atlas-create-free-cluster。

该工具需要 projectId、clusterName、provider 和 regions。

Create a dedicated Atlas cluster named myDedicatedCluster
in project myProject on AWS in US East

Non-Default Options

要指定非默认集群类型、实例大小或备份设置,请在提示中包含这些选项:

Create a dedicated Atlas cluster named myShardedCluster
in project myProject on AWS in US East with cluster type
SHARDED, instance size M30, and continuous backups enabled

多个区域

要在多个区域中分发集群,请在提示中命名最多三个区域。该工具将区域视为有序列表:第一个区域具有最高优先级,并保持主节点。该工具将可选举节点分发为一个区域中的三个节点、两个区域中的两个和一个节点,或三个区域中的两个、两个和一个节点。

Create a dedicated Atlas cluster named myMultiRegionCluster
in project myProject on AWS across US East, Ohio, and Ireland

使用客户托管密钥进行静态加密

要使用客户管理密钥加密集群,请在提示中命名密钥提供商。提供商必须已在项目中具有有效的静态加密配置。该工具选择现有配置,无法创建或修改配置。要了解如何配置密钥提供商,请参阅使用客户密钥管理进行静态加密。

如果您未命名提供商,则在存在有效配置的情况下,该工具会使用为集群的云提供商配置的客户管理密钥。否则,集群会使用默认的 Atlas 静态加密。

Create a dedicated Atlas cluster named myEncryptedCluster
in project myProject on AWS in US East, encrypted with our
AWS customer-managed key

atlas-upgrade-cluster 工具支持以下升级路径:

  • 自由灵活

  • 免费提供给 M10

  • Flex 至 M10

该工具还可将专用集群扩展到不同的 M10–M80实例大小,并更新其计算自动扩展设置。

连接到集群

如果您在 MCP 设置中配置了连接字符串,或者使用 atlas-connect-cluster 工具进行连接,则 MCP 服务器会从您的连接中推断集群名称、项目和当前层级。

Upgrade my current Atlas cluster to Flex

未连接集群

如果没有活动的集群连接,请在提示符中提供集群名称和项目。您可以选择指定目标层级、提供商和区域。

Upgrade my Atlas cluster named myFlexCluster
in project myProject to M10 on AWS in US East

扩展专用集群

要扩展专用集群,请至少指定以下选项之一:

  • 新实例大小

  • 是否启用计算自动伸缩

  • 自动扩展的最小或最大实例大小

Scale my Atlas cluster named myDedicatedCluster
in project myProject to M30 and let it autoscale
between M20 and M50

atlas-pause-resume-cluster 工具可以暂停或恢复专用 (M10+) Atlas 集群。免费和 Flex 集群不能暂停。

该工具需要以下参数:

  • projectId

  • clusterName

  • action. 将 action 设置为 PAUSE 或 RESUME。

暂停集群

暂停集群后,集群将无法连接,也不会产生计算费用。如果集群是当前活动连接,则 MCP 服务器会在集群暂停后自动从集群断开连接。

Pause cluster test-cluster-1 from project XXX

恢复集群

恢复集群后,集群不会立即可用于连接。使用 atlas-inspect-cluster 查询集群省/市/自治区,直到集群达到 IDLE。

Resume cluster test-cluster-1 from project XXX

以下示例演示如何使用MongoDB MCP 服务器来探索和了解MongoDB数据。

发现集群中有哪些数据库可用,并获取关于其内容的见解。

Show my Atlas databases

深入研究特定数据库,了解其结构和集合。

Show the collections in the Atlas sample_mflix database

分析MongoDB数据以确定模式和趋势。

Summarize the Atlas movies collection by thrillers

以下示例演示如何使用MongoDB MCP 服务器执行常见数据库操作。

向集合添加新数据。

Add a document to the movies_copy collection

创建和自定义新集合。

Create a new collection to store movie purchases data that
includes geospatial and timeseries fields

创建现有集合的备份或副本。

Make a copy of the movies collection named movies_copy

以下示例演示了如何导出查询结果以在外部工具中共享或进一步处理。

生成综合报告并将其导出以用于其他应用程序或与团队成员共享。

导出的结果将保存到运行 MCP 服务器的计算机上的文件中。您还可以使用AI客户端应用程序通过 exported-data资源访问权限导出的数据。

导出文件存储在 exportPath 配置选项指定的文件系统路径中。在 exportTimeout 配置选项指定的时间段后,可以删除导出文件。有关更多详细信息,请参阅从MongoDB MCP 服务器导出数据。

Summarize and export the contents of the movies collection.

您可以将 MCP 服务器与本地 Atlas 部署一起使用。要将 MCP 服务器工具用于本地Atlas部署,您必须安装Docker。有关本地Atlas部署的介绍,请参阅创建本地Atlas部署。

以下示例展示了如何使用MongoDB MCP 服务器与本地 Atlas 部署交互并创建本地 Atlas 部署。

以下示例列出了本地Atlas部署。

List all local Atlas deployments.

以下示例连接到名为 local7356 的本地Atlas部署并列出数据库。

Connect to my local Atlas deployment local7356 and list the
databases.

以下示例将创建一个新的本地Atlas部署,其中包含数据库和示例集合。要运行该示例,必须禁用只读模式。

Create a new local Atlas deployment, connect to it, create a
collection with sample pizza orders data, and then retrieve the
sample data.

以下示例删除名为 local5528 的本地Atlas部署。要运行该示例,必须禁用只读模式。

Delete my local Atlas deployment local5528.

atlas-local-delete-deployment 工具删除本地Atlas部署后,AI客户端可能会响应该部署已被删除。这是因为 atlas-local-delete-deployment 工具成功运行,并且本地Atlas部署不再存在。

以下示例演示如何使用MongoDB MCP 服务器识别和解决性能问题。提示的实际输出将取决于您的集群是否遇到性能问题,因此您的输出可能与示例不完全匹配。

这些示例使用 atlas-get-performance-advisor 工具从 Performance Advisor 中检索建议。

识别并分析性能缓慢的查询,以了解性能瓶颈。

注意

执行慢查询分析时, MongoDB MCP 服务器会检索慢查询示例,上限为 50 个查询。该示例最多包含 50 个符合提示中任何指定条件的最新慢速查询,以确保最佳性能和响应时间。

查询集群中的所有慢速查询:

Show me slow queries in my cluster

查询一般性能问题:

How is my cluster performance?

查询特定操作类型、持续时间或命名空间:

Show me slow writes for the past 24 hours in the movies collection

查询特定执行时间:

Show me queries longer than 5 seconds

使用Performance Advisor建议创建或删除索引,以提高性能。

What indexes should I create or drop to improve performance?

获取改进数据库模式设计和结构的建议。

Suggest schema improvements for my database

以下示例演示了如何使用MongoDB MCP服务器来使用MongoDB 向量搜索。要学习;了解有关 MCP 服务器如何与向量搜索配合使用的更多信息,请参阅向量搜索支持。

注意

要使用自动嵌入生成功能,您必须使用 Voyage AI API密钥 配置 MCP 服务器。

为您的集合创建、删除和列出向量搜索索引。

Create a vector search index on the sample_db.products collection
for the embeddings field with 1024 dimensions
using dot product similarity
Show me all vector search indexes on the sample_db.products collection
Drop the vector search index from the sample_db.products collection

配置 Voyage AI API密钥后,MCP 服务器会在插入文档时自动生成嵌入。您可以提供原始文本值,服务器为具有向量搜索索引的字段生成嵌入。

Insert documents into the sample_db database and products collection.
Use the voyage-3-large embedding model to generate
vector embeddings from descriptions for the `embeddings` field.
1. name: "Headphones", description: "Premium wireless noise-canceling headphones with 30-hour battery"
2. name: "Earbuds", description: "True wireless earbuds with active noise cancellation"
3. name: "Monitor", description: "24-inch 4K monitor with 1ms response time"
4. name: "Keyboard", description: "Mechanical keyboard with customizable RGB lighting"
5. name: "Mouse", description: "Wireless ergonomic mouse with programmable buttons"

配置 Voyage AI API密钥后,MCP 服务器会在运行搜索查询时自动生成嵌入。您可以提供原始文本,服务器会自动为查询生成嵌入。

Run a vector search query on sample_db.products on the 'embeddings' field
using the vector search index to find products that I can use to listen to music.

以下示例演示了如何通过MongoDB MCP Server 与MongoDB Search 配合使用。要学习;了解有关 MCP 服务器如何使用词法搜索的更多信息,请参阅词法搜索支持。

为集合创建、删除和列出词法搜索索引。

Create a lexical search index on the sample_db.products collection
for the description field
Show me all lexical search indexes on the sample_db.products collection
Drop the lexical search index from the sample_db.products collection

使用 $search聚合阶段通过 aggregate 工具运行词法搜索查询。

Run a lexical search query on sample_db.products on the 'description' field
using the lexical search index to find products that mention wireless.

使用带有 $searchMeta聚合阶段的 aggregate 工具返回有关词法搜索的元数据,例如匹配文档的计数,而不是匹配文档本身。

How many products have "wireless" in the description on
sample_db.products using the lexical search index

有关配置和使用MongoDB MCP 服务器的更多信息:

了解所有可用的配置选项和连接方法。

探索工具及其功能的完整列表。

了解如何安全地部署和使用 MCP 服务器。