Back to Blog
Guide
4 min read
April 15, 2026

How to Use MongoDB MCP with Claude Code

Query collections, run aggregations, manage indexes, and debug MongoDB documents directly from Claude Code using the MongoDB MCP.

mongodbnosqldatabaseaggregationclaude-code

What is the MongoDB MCP?

The MongoDB MCP gives Claude Code a direct connection to your MongoDB Atlas cluster or self-hosted MongoDB instance. Claude can query and aggregate documents, inspect collection schemas, manage indexes, and help you write or optimize aggregation pipelines — without opening MongoDB Compass or the Atlas UI.

Installation

mcpizy install mongodb

Provide your MongoDB connection string during setup. For Atlas clusters, use the SRV connection string from your Atlas dashboard. Ensure your IP is whitelisted in Atlas Network Access.

Key Capabilities

  • Query documents — run find queries with filters, projections, sort, and limit.
  • Run aggregation pipelines — write and execute multi-stage pipelines for analytics and transformations.
  • Inspect and create indexes — identify missing indexes and optimize query performance.
  • List collections and schemas — infer schema structure from sample documents.
  • Insert, update, and delete documents — manipulate data for testing and debugging.

Example Usage

Find the top 5 users by order count in the last 30 days:

db.orders.aggregate([
  { $match: { createdAt: { $gte: new Date(Date.now() - 30*24*60*60*1000) } } },
  { $group: { _id: "$userId", orderCount: { $sum: 1 } } },
  { $sort: { orderCount: -1 } },
  { $limit: 5 }
]);

Claude generates and runs this aggregation via the MCP, then explains the results.

Tips and Best Practices

  • Use a read-only database user for exploratory analysis — reserve write access for specific tasks.
  • Ask Claude to run explain() on slow queries to understand index usage before adding new indexes.
  • Combine with Redis MCP if you use MongoDB as your primary store and Redis for caching hot documents.
  • Use Atlas Search for full-text queries and ask Claude to build the search index definition for you.

View on MCPizy Marketplace

Found this useful? Share it.

MCP Servers Mentioned

MongodbPostgresRedis
All ArticlesBrowse Marketplace