Text Search Operators

Note

Views do not support text search.

Query Framework

Use the $text query operator to perform text searches on acollection with a text index.

$text will tokenize the search string using whitespace and mostpunctuation as delimiters, and perform a logical OR of all suchtokens in the search string.

For example, you could use the following query to find all storescontaining any terms from the list “coffee”, “shop”, and “java”:

  1. db.stores.find( { $text: { $search: "java coffee shop" } } )

Use the $meta query operator to obtain and sort by therelevance score of each matching document. For example, to order alist of coffee shops in order of relevance, run the following:

  1. db.stores.find(
  2. { $text: { $search: "coffee shop cake" } },
  3. { score: { $meta: "textScore" } }
  4. ).sort( { score: { $meta: "textScore" } } )

For more information on the $text and $metaoperators, including restrictions and behavior, see:

Aggregation Framework

When working with the Aggregation framework, use$match with a $text expression to execute a textsearch query. To sort the results in order of relevance score,use the$metaaggregation operator in the $sortstage [1].

For more information and examples of text search in theAggregation framework, seeText Search in the Aggregation Pipeline.

[1]The behavior and requirements of the $meta projectionoperator differ from that of the $meta aggregationoperator. For details on the $meta aggregation operator,see the $meta aggregation operator reference page.