NegationsPrefixing a word with a hyphen sign (-) negates a word:
- The negated word excludes documents that contain the negated word from the result set.
- When passed a search string that only contains negated words, text search will not match any documents.
- A hyphenated word, such as pre-market, is not a negation. The $text operator treats the hyphen as a delimiter.
The $text operator adds all negations to the query with the logical AND operator.
Match OperationThe $text operator ignores language-specific stop words, such as the and and in English.
The $text operator matches on the complete stemmed word. So if a document field contains the wordblueberry, a search on the term blue will not match. However, blueberry or blueberries will match.
For non-diacritics, text search is case insensitive; i.e. case insensitive for [A-z].
Text ScoreThe $text operator assigns a score to each document that contains the search term in the indexed fields. The score represents the relevance of a document to a given text search query. The score can be part of asort() method specification as well as part of the projection expression. The { $meta:"textScore" } expression provides information on the processing of the $text operation. See $metaprojection operator for details on accessing the score for projection or sort.
ExamplesThe following examples assume a collection articles that has a text index on the field subject:
[C#] 纯文本查看 复制代码
db.articles.ensureIndex( { subject: "text" } )
Search for a Single WordThe following query searches for the term coffee:
[C#] 纯文本查看 复制代码
db.articles.find( { $text: { $search: "coffee" } } )
This query returns documents that contain the term coffee in the indexed subject field.
Match Any of the Search TermsIf the search string is a space-delimited string, $text operator performs a logical OR search on each term and returns documents that contains any of the terms.
The following query searches specifies a $search string of three terms delimited by space, "bakecoffee cake":
[C#] 纯文本查看 复制代码
db.articles.find( { $text: { $search: "bake coffee cake" } } )
This query returns documents that contain either bake or coffee or cake in the indexed subject field.
Search for a PhraseTo match the exact phrase as a single term, escape the quotes.
The following query searches for the phrase coffee cake:
[C#] 纯文本查看 复制代码
db.articles.find( { $text: { $search: "\"coffee cake\"" } } )
This query returns documents that contain the phrase coffee cake.
[C#] 纯文本查看 复制代码
SEE ALSO
Phrases
Exclude Documents That Contain a TermA negated term is a term that is prefixed by a minus sign -. If you negate a term, the $text operator will exclude the documents that contain those terms from the results.
The following example searches for documents that contain the words bake or coffee but do not contain the term cake:
[C#] 纯文本查看 复制代码
db.articles.find( { $text: { $search: "bake coffee -cake" } } )
SEE ALSO
Negations
Return the Text Search ScoreThe following query searches for the term cake and returns the score assigned to each matching document:
[C#] 纯文本查看 复制代码
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
)
In the result set, the returned documents includes an additional field score that contains the document’s score associated with the text search. [1]
SEE ALSO
Text Score
Sort by Text Search ScoreTo sort by the text score, include the same $meta expression in both the projection document and the sort expression. [1] The following query searches for the term cake and sorts the results by the descending score:
[C#] 纯文本查看 复制代码
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
In the result set, the returned documents includes an additional field score that contains the document’s score associated with the text search.
SEE ALSO
Text Score
Return Top 3 Matching DocumentsUse the limit() method in conjunction with a sort() to return the top three matching documents. The following query searches for the term cake and sorts the results by the descending score:
[C#] 纯文本查看 复制代码
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } ).limit(3)
SEE ALSO
Text Score
Text Search with Additional Query and Sort ExpressionsThe following query searches for documents with status equal to "A" that contain the terms coffee orcake in the indexed field subject and specifies a sort order of ascending date, descending text score:
[C#] 纯文本查看 复制代码
db.articles.find(
{ status: "A", $text: { $search: "coffee cake" } },
{ score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )
Search a Different LanguageUse the optional $language field in the $text expression to specify a language that determines the list of stop words and the rules for the stemmer and tokenizer for the search string.
If you specify a language value of "none", then the text search uses simple tokenization with no list of stop words and no stemming.
The following query specifies es for Spanish as the language that determines the tokenization, stemming, and stop words:
[C#] 纯文本查看 复制代码
db.articles.find(
{ $text: { $search: "leche", $language: "es" } }
)
The $text expression can also accept the language by name, spanish. See Text Search Languages for the supported languages.