Back to Codex
Databases·
intermediate
·9 min read·Apr 4, 2026

How 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

ToolDescription
code
find
Query documents with filters
code
aggregate
Run aggregation pipelines
code
list_collections
List all collections
code
collection_schema
Infer schema from documents
code
count
Count matching documents
code
insert_one
Insert a document
code
update_one
Update a document
code
delete_one
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

  1. Create a dedicated user with only the necessary database permissions
  2. Use Atlas IP whitelist to restrict access
  3. 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
    code
    collection_schema
    to help the AI understand your data model
  • For large datasets, the server automatically applies reasonable limits