Choose the Aggregation Pipeline Stage

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

Choose the Aggregation Pipeline Stage

MongoDB Search queries run inside an [aggregation pipeline stage](https://www.mongodb.com/docs/manual/aggregation.md#std-label-aggregation, which is the preferred method for performing aggregations.

Aggregation operations process multiple documents and return computed results. You can use aggregation operations to:

  • Group values from multiple documents together.

  • Perform operations on the grouped data to return a single result.

  • Analyze data changes over time.

You can use either the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search or the [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta stage as the first stage in the aggregation pipeline.

Aggregation Pipeline Stage Description
[$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search Conducts full-text searches and returns an ordered list of documents along with additional search metadata. Use [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search to retrieve matching documents with or without facets.
[$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta Conducts full-text searches and returns the metadata without fetching the documents. Use [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta to retrieve only the metadata about your search results, such as the total count of matching documents or facets.

After the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search or the [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta stage completes, you can use additional aggregation stages to process documents further. For example, you can use one or more of the following stages, in addition to other stages:

Aggregation Pipeline Stage Description
[$limit](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit Limits the number of documents passed to the next stage in the pipeline.
[$skip](https://www.mongodb.com/docs/manual/reference/operator/aggregation/skip.md#mongodb-pipeline-pipe.-skip Skips documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.
[$project](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project Passes along documents with the requested fields to the next stage in the pipeline.
[$addFields](https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields.md#mongodb-pipeline-pipe.-addFields Adds new fields to documents.
[$facet](https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet.md#mongodb-pipeline-pipe.-facet Processes multiple aggregation pipelines within a single stage on the same set of input documents.
[$match](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match Filters documents based on a specified query predicate and passes matched documents to the next pipeline stage.
[$group](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group Combines documents with the same fields or expression into a single document by a group key.
[$lookup](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-lookup Performs a left outer join to a collection in the same database to filter in documents from the “joined” collection for processing.

When you add these stages to your aggregation pipeline, consider the potentially significant performance impact.

To improve performance, we recommend that you use [$limit](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit for search results, [paginate](https://www.mongodb.com/docs/search/query/paginate-results.md#std-label-fts-paginate-results through search results as needed, and [retrieve search results after a reference point.](https://www.mongodb.com/docs/search/query/paginate-results.md#std-label-paginate-results-search-after