aggregate
Run data processing pipelines on MongoDB collections to filter, transform, and analyze documents.
Instructions
Run an aggregation against a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| pipeline | Yes | An 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` subpipeline. 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 approprate 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/ | |
| collection | Yes | Collection name | |
| responseBytesLimit | No | The maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. Note to LLM: If the entire aggregation result is required, use the "export" tool instead of increasing this limit. |