$searchThe
$searchstage performs a full-text search on the specified field or fields. The field or fields must be covered by an MongoDB Search index.
Syntax
A $search pipeline stage has the following prototype form:
{ $search: { "index": "<index-name>", "<operator-name>"|"<collector-name>": { <operator-specification>|<collector-specification> }, "highlight": { <highlight-options> }, "concurrent": true | false, "count": { <count-options> }, "searchAfter"|"searchBefore": "<encoded-token>", "scoreDetails": true| false, "sort": { <fields-to-sort>: 1 | -1 }, "returnStoredSource": true | false, "tracking": { <tracking-option> } } }
Fields
The $search stage takes a document with the following fields:
Field | Type | Necessity | Description |
|---|---|---|---|
| object | Conditional | Name of the collector to use with the
query. You can provide a document that contains the
collector-specific options as the value for this field. Either
this or |
| boolean | Optional | Parallelize search across segments on dedicated search
nodes. If you don't have separate search
nodes on your cluster, MongoDB Search ignores this flag. If omitted,
defaults to |
| object | Optional | Document that specifies the count options for retrieving a count of the results. To learn more, see Count MongoDB Search Results. |
| object | Optional | Document that specifies the highlighting options for displaying search terms in their original context. |
| string | Optional | Name of the MongoDB Search index to use. If omitted, defaults to
If you name your index MongoDB Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster. |
| object | Conditional | |
| boolean | Optional | Flag that specifies whether to perform a full document lookup
on the backend database or return only stored source fields
directly from MongoDB Search. If omitted, defaults to |
| string | Optional | Reference point for retrieving results. |
| string | Optional | Reference point for retrieving results. |
| boolean | Optional | Flag that specifies whether to retrieve a detailed breakdown of
the score for the documents in the results. If omitted, defaults
to |
| object | Optional | Document that specifies the fields to sort the MongoDB Search results by in ascending or descending order. You can sort by date, number (integer, float, and double values), and string values. To learn more, see Sort MongoDB Search Results. |
| object | Optional | Document that specifies the tracking option to retrieve analytics information on the search terms. WARNING: This is now deprecated. The official end-of-life and complete removal of support will take effect on December 5, 2025. Therefore, Atlas will no longer collect query data for display in the Atlas UI starting on this date and the UI for this feature will be removed. If you have any questions or need further assistance, contact support or your Account Executive. |
Behavior
$search must be the first stage of any pipeline it appears
in. $search cannot be used in:
a
$facetpipeline stage
Aggregation Variable
$search returns only the results of your query. The
metadata results of your $search query are saved in the
$$SEARCH_META aggregation variable. You can use the
$$SEARCH_META variable to view the metadata results for your
$search query.
The $$SEARCH_META aggregation variable
can be used anywhere after a $search stage in any pipeline,
but it can't be used after the $lookup or
$unionWith stage in any pipeline. The $$SEARCH_META
aggregation variable can't be used in any subsequent stage after a
$searchMeta stage.
Example
Suppose the following index on the sample_mflix.movies
collection.
{ "mappings": { "dynamic": false, "fields": { "released": { "type": "date" } } } }
The following query searches for movies released near September
01, 2011 using the $search stage. The query includes
a:
$projectstage to exclude all fields in the documents excepttitleandreleased.$facetstage that outputs a:docsfield with an array of the top5search resultsmetafield with the value of$$SEARCH_METAvariable
db.movies.aggregate([ { "$search": { "near": { "path": "released", "origin": ISODate("2011-09-01T00:00:00.000+00:00"), "pivot": 7776000000 } } }, { $project: { "_id": 0, "title": 1, "released": 1 } }, { "$limit": 5 }, { "$facet": { "docs": [], "meta": [ {"$replaceWith": "$$SEARCH_META"}, {"$limit": 1} ] } } ])
{ "docs" : [ { "title" : "Submarino", "released" : ISODate("2011-09-01T00:00:00Z") }, { "title" : "Devil's Playground", "released" : ISODate("2011-09-01T00:00:00Z") }, { "title" : "Bag It", "released" : ISODate("2011-09-01T00:00:00Z") }, { "title" : "Dos", "released" : ISODate("2011-09-01T00:00:00Z") }, { "title" : "We Were Here", "released" : ISODate("2011-09-01T00:00:00Z") } ], "meta" : [ { "count" : { "lowerBound" : NumberLong(17373) } } ] }
To learn more about the $$SEARCH_META variable and its usage,
see:
Troubleshooting
If you are experiencing issues with your MongoDB Search $search
queries, see Troubleshoot Queries.