Docs Menu
Docs Home
/ /

$text Query Operators

Note

MongoDB offers an improved full-text search solution, MongoDB Search, and semantic search solution, MongoDB Vector Search. We recommend using the $search, $searchMeta, or $vectorSearch stages, instead of the $text operator.

You can use the $text operator on a collection with a text index.

$text tokenizes the search string using whitespace and most punctuation as delimiters, and performs a logical OR of all such tokens in the search string.

For example, you could use the following query to find all stores containing any terms from the list "coffee", "shop", and "java" in the stores collection:

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

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

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

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

When working with aggregation pipelines, use $match with a $text expression. To sort the results in order of relevance score, use the $meta aggregation operator in the $sort stage.

For more information and examples, see $text in the Aggregation Pipeline.

MongoDB Search provides the $search aggregation stage to perform full-text search on your collections.

Back

Perform a $text Query

On this page