Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

aggregate

Read-only

Run a MongoDB aggregation pipeline to filter, group, transform, and search documents, supporting vector and full-text search.

Instructions

Run an aggregation against a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
pipelineYesAn array of aggregation stages to execute. If the user has asked for a vector search, `$vectorSearch` **MUST** be the first stage of the pipeline (or the first stage of a `$unionWith` sub-pipeline only when explicitly combining unrelated result sets — for hybrid full-text + vector search, use `$rankFusion` or `$scoreFusion` instead, see below). If the user has asked for lexical/Atlas search, use `$search` instead of `$text`. ### Usage Rules for `$vectorSearch` - **Index Type Detection:** Use the collection-indexes tool to determine if the target field has a classic vector index (type: 'vector') or an auto-embed index (type: 'autoEmbed'). - **Classic Vector Search (type: 'vector'):** Use 'queryVector' with embeddings as an array of numbers. - **Auto-Embed Vector Search (type: 'autoEmbed'):** Use 'query' - MongoDB automatically generates embeddings at query time. Do NOT use 'queryVector' or 'embeddingParameters' for auto-embed indexes. - **Unset embeddings:** Unless the user explicitly requests the embeddings, add an `$unset` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**. - **Pre-filtering:** If the user requests additional filtering, include filters in `$vectorSearch.filter` only for pre-filter fields in the vector index. NEVER include fields in $vectorSearch.filter that are not part of the vector index. - **Post-filtering:** For all remaining filters, add a $match stage after $vectorSearch. - If unsure which fields are filterable, use the collection-indexes tool to determine valid prefilter fields. - If no requested filters are valid prefilters, omit the filter key from $vectorSearch. ### Usage Rules for `$search` - Include the index name, unless you know for a fact there's a default index. If unsure, use the collection-indexes tool to determine the index name. - The `$search` stage supports multiple operators, such as 'autocomplete', 'text', 'geoWithin', and others. Choose the appropriate operator based on the user's query. If unsure of the exact syntax, consult the MongoDB Atlas Search documentation, which can be found here: https://www.mongodb.com/docs/atlas/atlas-search/operators-and-collectors/ ### Usage Rules for `$rankFusion` and `$scoreFusion` (Hybrid Search) Use these stages when the user wants to combine full-text (`$search`) and vector (`$vectorSearch`) retrieval into a single fused result set. **Prefer native fusion over a `$unionWith` + `$group` workaround** — the workaround averages incompatible score scales and produces wrong rankings. **Which stage to use:** - `$rankFusion` (MongoDB 8.0+) — Reciprocal Rank Fusion. The recommended default. Normalizes scores across incompatible scales automatically. No score tuning needed. - `$scoreFusion` (MongoDB 8.2+) — Score-based fusion. Use when the user needs explicit per-pipeline weights, score normalisation (sigmoid / minMaxScaler), or a custom combination expression. **Construction rules:** - `$rankFusion` / `$scoreFusion` MUST be the first stage of the top-level pipeline. - Sub-pipelines go inside `input.pipelines` as a named map (not an array). Each name must be non-empty, must not start with `$`, and must not contain `.` or null bytes. - Allowed stages inside sub-pipelines: `$search`, `$vectorSearch`, `$match`, `$sort`, `$geoNear`, `$skip`, `$limit`. `$project` and `$unset` are NOT allowed inside sub-pipelines. - Do field shaping (`$project` / `$unset`) only AFTER the fusion stage, at the root. - Both a vectorSearch (or autoEmbed) index AND a search (lexical) index must exist on the collection. Use the collection-indexes tool to confirm both before running a hybrid query. - Add a `$limit` stage after the fusion stage to cap the final result set. - Add `$unset` at the end to remove embedding fields and avoid context bloat. ### Usage Rules for `$rerank` (Native Reranking) Use this stage when the user wants to reorder a set of candidate documents using a cross-encoder reranker model. **Construction rules:** - `$rerank` can be any stage in the pipeline on an Atlas cluster running MongoDB 8.3 or higher. - It is recommended to use `$rerank` after a sorted pipeline, e.g. `$search`, `$vectorSearch`, `$rankFusion`, `$scoreFusion`, or [`$match`, `$sort`]. - $rerank must be enabled via the Native Reranking Project Setting - Set `numDocsToRerank` as the number of documents passed into `$rerank`. This will also limit the number of documents returned by `$rerank` - Set `path` as a field name or an array of field names that exist in all documents. Use `$match` or `$set` before `$rerank` to validate no fields are missing. - Add `$addFields` after `$rerank` to retrieve the reranker score. **`$rerank` example (recommended default):** ```javascript [ { $match: { description: { $exists: true }, name: { $exists: true } } }, { $sort: { lastUpdated: -1 } }, { $rerank: { query: { text: "query text including instructions" }, model: "rerank-2.5", numDocsToRerank: 100, path: ["description", "name"] } }, { $addFields: { rerankScore: { $meta: "score" } } } ] ```
collectionYesCollection name
connectionIdYesThe connection to run the operation against. Use the id returned by one of the connect tools, or "preconfigured" to use the connection string the server was configured with.
responseBytesLimitNoThe maximum number of bytes to return in the response. This value is capped by the server's configured maximum and cannot be exceeded.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
countYesThe total number of documents returned by the aggregation pipeline
documentsYesThe documents returned by the aggregation pipeline
appliedLimitsYesThe limits applied to the aggregation pipeline
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true and destructiveHint=false, so the safety profile is clear. The description itself adds no additional behavioral context, though the schema's pipeline description does disclose mandatory $unset stages and response size limits, which are not in annotations. The description alone is transparent but minimal.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The main description is concise but minimal, while the pipeline parameter description is extremely long (several hundred words), albeit well-organized with section headers and an example. Some instructions are repeated (e.g., 'use the collection-indexes tool') and the length may challenge readability, though it is justified by the tool's complexity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

An output schema exists, and the pipeline description covers advanced features (vector search, hybrid fusion, reranking) and mentions responseBytesLimit. However, the tool-level description lacks a high-level overview of what aggregation can do or when to prefer it over simpler tools, leaving some context to the AI agent's prior knowledge.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The pipeline parameter description is exceptionally detailed, covering $vectorSearch with classic and auto-embed variants, $search, $rankFusion, $scoreFusion, $rerank, pre/post-filtering rules, and mandatory $unset. This goes far beyond the basic schema and provides essential operational guidance for complex aggregations.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Run an aggregation against a MongoDB collection' uses a specific verb+resource and clearly distinguishes from the sibling tool 'aggregate-db' by explicitly targeting a collection. It unambiguously communicates the core operation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description does not explicitly state when to use this tool versus alternatives like 'find' or 'count', and no exclusions are mentioned. The extensive pipeline rules in the schema cover how to build an aggregation but not when to choose this tool over siblings, so usage is only implied.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mongodb-js/mongodb-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server