Databases·
intermediate
·9 min read·Apr 4, 2026How to Use the MongoDB MCP Server for NoSQL Data Access
Connect MongoDB databases to AI agents for document querying, aggregation pipelines, and schema exploration using MCP.
MongoDBNoSQLdatabasedocumentsaggregation
Use the MongoDB MCP Server
The MongoDB MCP server enables AI agents to query and analyze data in MongoDB databases, supporting find operations, aggregation pipelines, and schema exploration.
Prerequisites
- ▸MongoDB instance running (local or Atlas)
- ▸Connection string
- ▸Node.js 18+
Configuration
json
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mcp-server-mongodb"],
"env": {
"MONGODB_URI": "mongodb:///mydb"
}
}
}
}MongoDB Atlas Connection
json
{
"env": {
"MONGODB_URI": "mongodb+srv://user:password@cluster.mongodb.net/mydb?retryWrites=true&w=majority"
}
}Available Tools
| Tool | Description |
|---|---|
code | Query documents with filters |
code | Run aggregation pipelines |
code | List all collections |
code | Infer schema from documents |
code | Count matching documents |
code | Insert a document |
code | Update a document |
code | Delete a document |
Example Queries
Basic Queries
code
"Find all users who signed up in the last 30 days"Aggregation Analysis
code
"Create an aggregation pipeline to show revenue by product category per month"Schema Exploration
code
"What collections exist in the database and what do their schemas look like?"Security Best Practices
- ▸Create a dedicated user with only the necessary database permissions
- ▸Use Atlas IP whitelist to restrict access
- ▸Enable audit logging to track AI agent queries
javascript
// MongoDB shell: create a read-only user
db.createUser({
user: "mcp_reader",
pwd: "secure_password",
roles: [{ role: "read", db: "mydb" }]
});Tips
- ▸The AI can help design aggregation pipelines for complex analysis
- ▸Use to help the AI understand your data modelcode
collection_schema - ▸For large datasets, the server automatically applies reasonable limits