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

NameRequiredDescriptionDefault
collectionYesCollection name
databaseNoDatabase name (optional if default database is configured)
limitNoMaximum number of documents to return (optional)
pipelineYesMongoDB aggregation pipeline stages (read-only operations only)

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name (optional if default database is configured)", "type": "string" }, "limit": { "description": "Maximum number of documents to return (optional)", "maximum": 1000, "minimum": 1, "type": "number" }, "pipeline": { "description": "MongoDB aggregation pipeline stages (read-only operations only)", "items": { "type": "object" }, "type": "array" } }, "required": [ "collection", "pipeline" ], "type": "object" }