Text Search
On this page
Overview
MongoDB supports query operations that perform a text search of string content. To perform text search, MongoDB uses atext indexand the$text
operator.
NOTE
Viewsdo not support text search.
Example
This example demonstrates how to build a text index and use it to find coffee shops, given only text fields.
Create a collectionstores
with the following documents:
db.stores.insert(
[
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
]
)
Text Index
MongoDB providestext indexesto support text search queries on string content.text
indexes can include any field whose value is a string or an array of string elements.
To perform text search queries, you must have atext
index on your collection. A collection can only haveonetext search index, but that index can cover multiple fields.
For example you can run the following in amongo
shell to allow text search over thename
anddescription
fields:db
db.stores.createIndex( { name: "text", description: "text" } )
$text
Operator
Use the$text
query operator to perform text searches on a collection with atext index.
$text
will tokenize the search string using whitespace and most punctuation as delimiters, and perform a logicalOR
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”:
db.stores.find( { $text: { $search: "java coffee shop" } } )
Exact Phrase
You can also search for exact phrases by wrapping them in double-quotes. For example, the following will find all documents containing “java” or “coffee shop”:
db.stores.find( { $text: { $search: "java \"coffee shop\"" } } )
Term Exclusion
To exclude a word, you can prepend a “-
” character. For example, to find all stores containing “java” or “shop” but not “coffee”, use the following:db
db.stores.find( { $text: { $search: "java shop -coffee" } } )
Sorting
MongoDB will return its results in unsorted order by default. However, text search queries will compute a relevance score for each document that specifies how well a document matches the query.
To sort the results in order of relevance score, you must explicitly project the$meta
textScore
field and sort on it:
db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
Text search is also available in the aggregation pipeline.
Language Support
MongoDB supports text search for various languages. SeeText Search Languagesfor a list of supported languages.