MongoDB MCP Server
aggregate
Execute a read-only aggregation pipeline on a collection.
Supported Stages:
- $match: Filter documents
- $group: Group documents by a key
- $sort: Sort documents
- $project: Shape the output
- $lookup: Perform left outer joins
- $unwind: Deconstruct array fields
Unsafe/Blocked Stages:
- $out: Write results to collection
- $merge: Merge results into collection
- $addFields: Add new fields
- $set: Set field values
- $unset: Remove fields
- $replaceRoot: Replace document structure
- $replaceWith: Replace document
Example - User Statistics by Role: use_mcp_tool with server_name: "mongodb", tool_name: "aggregate", arguments: { "collection": "users", "pipeline": [ { "$match": { "active": true } }, { "$group": { "_id": "$role", "count": { "$sum": 1 }, "avgAge": { "$avg": "$age" } }}, { "$sort": { "count": -1 } } ], "limit": 100 }
Example - Posts with Author Details: use_mcp_tool with server_name: "mongodb", tool_name: "aggregate", arguments: { "collection": "posts", "pipeline": [ { "$match": { "published": true } }, { "$lookup": { "from": "users", "localField": "authorId", "foreignField": "_id", "as": "author" }}, { "$unwind": "$author" }, { "$project": { "title": 1, "authorName": "$author.name", "publishDate": 1 }} ] }
Input Schema
Name | Required | Description | Default |
---|---|---|---|
collection | Yes | Collection name | |
database | No | Database name (optional if default database is configured) | |
limit | No | Maximum number of documents to return (optional) | |
pipeline | Yes | MongoDB aggregation pipeline stages (read-only operations only) |