Note
This page describes text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search and a vector search solution, Vector Search.本页介绍自我管理(非Atlas)部署的文本查询功能。对于托管在MongoDB上的数据,MongoDB还提供了改进的全文查询解决方案MongoDB搜索和矢量搜索解决方案矢量搜索。
This page describes the 本页介绍用于自我管理部署的$text operator for self-managed deployments.$text运算符。
Definition定义
$text$textperforms a text query on the content of the fields indexed with a text index.对用文本索引索引的字段的内容执行文本查询。
Compatibility兼容性
You can use 您可以将$text for deployments hosted in the following environments:$text用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
A $text expression has the following syntax:$text表达式具有以下语法:
{
$text: {
$search: <string>,
$language: <string>,
$caseSensitive: <boolean>,
$diacriticSensitive: <boolean>
}
}
The $text operator accepts a text query document with the following fields:$text运算符接受具有以下字段的文本查询文档:
$search | OR query on the terms unless you specify the terms as an exact string. OR查询,除非您将术语指定为精确的字符串。 | |
$language |
| |
$caseSensitive |
| |
$diacriticSensitive |
|
The 默认情况下,$text operator, by default, does not return results sorted in terms of the results' scores. $text运算符不会返回按结果分数排序的结果。For more information on sorting by the results' scores, see the Text Score documentation.有关按结果分数排序的更多信息,请参阅文本分数文档。
Behavior行为
Restrictions限制
A query can specify, at most, one一个查询最多只能指定一个$textexpression.$text表达式。$textcannot appear in$norexpressions.$text不能出现在$nor表达式中。$textcannot appear in$elemMatchquery expressions or$elemMatchprojection expressions.$text不能出现在$elemMatch查询表达式或$elemMmatch投影表达式中。To use要在$textin an$orexpression, all clauses in the$orarray must be indexed.$or表达式中使用$text,必须对$or数组中的所有子句进行索引。If a query includes a如果查询包含$textexpression, you cannot usehint()to specify which index to use for the query.$text表达式,则不能使用hint()指定查询使用的索引。You cannot specify the如果查询包含$naturalsort order if the query includes a$textexpression.$text表达式,则无法指定$natural排序顺序。You cannot combine the您不能将需要特殊文本索引的$textexpression, which requires a special text index, with a query operator that requires a different type of special index.$text表达式与需要不同类型特殊索引的查询运算符组合在一起。For example you cannot combine例如,您不能将$textexpression with the$nearoperator.$text表达式和$near运算符组合在一起。Views do not support视图不支持$text.$text。$textis unsupported for creating indexes using the Stable API V1.$text不支持使用Stable API V1创建索引。
If using the 如果在聚合中使用$text operator in aggregation, the following restrictions also apply.$text运算符,则以下限制也适用。
The包含$matchstage that includes a$textmust be the first stage in the pipeline.$text的$match阶段必须是管道中的第一阶段。A$textoperator can only occur once in the stage.$text运算符在阶段中只能出现一次。The$textoperator expression cannot appear in$oror$notexpressions.$text运算符表达式不能出现在$or或$not表达式中。默认情况下,$text, by default, does not return the matching documents in order of matching scores.$text不会按匹配分数的顺序返回匹配的文档。To sort by descending score, use the要按得分降序排序,请在$metaaggregation expression in the$sortstage.$sort阶段使用$meta聚合表达式。
$search Field字段
In the 在$search field, specify a string of words that the $text operator parses and uses to query the text index.$search字段中,指定$text运算符解析并用于查询文本索引的字符串。
The $text operator treats most punctuation in the string as delimiters, except a hyphen-minus (-) that negates term or an escaped double quotes \" that specifies an exact string.$text运算符将字符串中的大多数标点符号视为分隔符,除了否定术语的连字符减号(-)或指定精确字符串的转义双引号\"。
Note
The $search field for the $text expression is different than the $search aggregation stage provided by Atlas Search. $text表达式的$search字段与Atlas search提供的$search聚合阶段不同。The $search aggregation stage performs a full-text search on specified fields and is only available on MongoDB Atlas.$search聚合阶段对指定字段执行全文搜索,仅在MongoDB Atlas上可用。
Exact Strings精确字符串
To match on an exact multi-word string, as opposed to individual terms, enclose the string in escaped double quotes (要匹配精确的多单词字符串,而不是单个术语,请将字符串括在转义双引号(\"), as in:\")中,如:
"\"ssl certificate\""
If the 如果$search string of a $text operation includes a multi-word string and individual terms, $text only matches the documents that include the multi-word string.$text操作的$search字符串包括多单词字符串和单个术语,则$text仅匹配包含多单词字符串的文档。
For example, the following 例如,以下$search string returns documents that include the exact string "ssl certificate":$search字符串返回包含确切字符串"ssl certificate"的文档:
"\"ssl certificate\" authority key"Negations否定
Prefixing a word with a hyphen-minus (在单词前加连字符减(-) negates a word:-)可以否定单词:
The negated word excludes documents that contain the negated word from the result set.否定词将包含否定词的文档从结果集中排除。When passed a string that only contains negated words,当传递一个只包含否定词的字符串时,$textdoes not match any documents.$text与任何文档都不匹配。A hyphenated word, such as连字符的单词,如pre-market, is not a negation. If used in a hyphenated word, the$textoperator treats the hyphen-minus (-) as a delimiter.pre-market,不是否定。如果在连字符单词中使用,$text运算符会将连字符减(-)视为分隔符。To negate the word在这种情况下,为了否定marketin this instance, include a space betweenpreand-market, i.e.,pre -market.market一词,在pre和-market之间加一个空格,即pre -market。
The $text operator adds all negations to the operation with the logical AND operator.$text运算符使用逻辑AND运算符将所有否定添加到操作中。
Match Operation匹配操作
Stop Words停用词
The $text operator ignores language-specific stop words, such as the and and in English.$text运算符忽略特定语言的停用词,如英语中的the和and。
Stemmed Words词干词
When you use case insensitivity and diacritic insensitivity, the 当您使用不区分大小写和不区分变音符号时,$text operator matches on the complete stemmed word. $text运算符会匹配完整的词干单词。If a document field contains the word 如果文档字段包含单词blueberry, a $text operation with a $search term of blue does not match. blueberry(蓝莓),则具有blue的$search术语的$text操作不匹配。However, 然而,blueberry or blueberries match.blueberry或blueberries匹配。
Case Sensitivity and Stemmed Words区分大小写和词干
When you use case sensitivity (当您使用区分大小写($caseSensitive: true), if the suffix stem contains uppercase letters, the $text operator matches on the exact word.$caseSensitive:true)时,如果后缀词干包含大写字母,则$text运算符将与确切的单词匹配。
Diacritic Sensitivity and Stemmed Words变调敏感与词干
When you use diacritic sensitivity (当您使用变音符号敏感性($diacriticSensitive: true), if the suffix stem contains the diacritic mark or marks, the $text operator matches on the exact word.$diacriticSensitive: true)时,如果后缀词干包含一个或多个变音符号标记,则$text运算符将与确切的单词匹配。
Case Insensitivity病例不敏感
The $text operator defaults to the case insensitivity of the text index:$text运算符默认为文本索引不区分大小写:
The version 3 text index is case insensitive for Latin characters with or without diacritics and characters from non-Latin alphabets, such as the Cyrillic alphabet. See text index for details.版本3的文本索引对带有或不带有变音符号的拉丁字符以及非拉丁字母(如西里尔字母)的字符不区分大小写。详见文本索引。Earlier versions of the早期版本的textindex are case insensitive for Latin characters without diacritic marks; i.e. for[A-z].text索引对没有变音符号的拉丁字符不区分大小写;即[A-z]。
$caseSensitive Option选项
To support case sensitivity where the 要在text index is case insensitive, specify $caseSensitive: true.text索引不区分大小写的情况下支持区分大小写,请指定$caseSensitive:true。
Case Sensitivity Process大小写敏感性过程
If 如果$caseSensitive: true and the text index is case insensitive, the $text operator:$caseSensitive: true且文本索引不区分大小写,则$text运算符:
First queries the首先查询textindex for case insensitive and diacritic matches.text索引以查找不区分大小写和变音符号的匹配项。Then, to return just the documents that match the case of the specified terms, the然后,为了只返回与指定术语大小写匹配的文档,$textoperation includes an additional stage to filter out the documents that do not match the specified case.$text操作包括一个额外的阶段,用于筛选出与指定大小写不匹配的文档。
If 如果$caseSensitive: true and if the suffix stem contains uppercase letters, the $text operator matches on the exact word.$caseSensitive: true,并且后缀词干包含大写字母,则$text运算符与确切的单词匹配。
Specifying 指定$caseSensitive: true may impact performance.$caseSensitive: true可能会影响性能。
Tip
Diacritic Insensitivity变音符号不敏感
The $text operator defaults to the diacritic insensitivity of the text index:$text运算符默认为文本索引对变音符号不敏感:
The version 3 text index is diacritic insensitive.版本3的text索引对变音符号不敏感。That is, the index does not distinguish between characters that contain diacritical marks and their non-marked counterpart, such as也就是说,索引不会区分包含变音符号的字符和它们的非标记对应字符,如é,ê, ande.é、ê和e。Earlier versions of the早期版本的textindex are diacritic sensitive.text索引对变音符号敏感。
$diacriticSensitive Option选项
To support diacritic sensitivity with the 要使用text index, specify $diacriticSensitive: true.text索引支持变音符号敏感性,请指定$diacriticSensitive: true。
Text queries against earlier versions of the 针对早期版本的文本索引的文本查询本质上对变音符号敏感,不能对变音字符不敏感。因此,text index are inherently diacritic sensitive and cannot be diacritic insensitive. As such, the $diacriticSensitive option for the $text operator has no effect with earlier versions of the text index.$text运算符的$diacriticSensitive选项对早期版本的文本索引无效。
Diacritic Sensitivity ProcessDiacritic敏感性过程
To use diacritic sensitivity (要将变音符号敏感性($diacriticSensitive: true) with a version 3 text index, the $text operator:$diacriticSensitive: true)与版本3文本索引一起使用,$text运算符:
First queries the首先查询textindex, which is diacritic insensitive.text索引,该索引对变音符号不敏感。Then, to return just the documents that match the diacritic marked characters of the specified terms, the然后,为了只返回与指定术语的变音符号标记字符匹配的文档,$textoperation includes an additional stage to filter out the documents that do not match.$text操作包括一个额外的阶段来筛选不匹配的文档。
Specifying 指定$diacriticSensitive: true may impact performance.$diacriticSensitive: true可能会影响性能。
If you use 如果将$diacriticSensitive: true with an earlier version of the text index, the $text operator queries the text index, which is diacritic sensitive.$diacriticSensitive: true与早期版本的text索引一起使用,则$text运算符会查询文本索引,该索引对变音符号敏感。
If 如果$diacriticSensitive: true and if the suffix stem contains the diacritic mark or marks, the $text operator matches on the exact word.$diacriticSensitive: true,并且后缀词干包含一个或多个变音符号,则$text运算符将与确切的单词匹配。
Tip
Text Score文本得分
The $text operator assigns a score to each result document. The score represents the relevance of a document to a given query. $text运算符为每个结果文档分配一个分数。分数表示文档与给定查询的相关性。The score can be part of a 分数可以是sort() method specification as well as part of the projection expression. sort()方法规范的一部分,也可以是投影表达式的一部分。The { $meta: "textScore" } expression provides information on the processing of the $text operation. { $meta: "textScore" }表达式提供了关于$text操作处理的信息。See 有关访问投影或排序分数的详细信息,请参阅$meta projection operator for details on accessing the score for projection or sort.$meta投影运算符。
Examples示例
The following examples assume a collection 以下示例假设一个集合articles that has a version 3 text index on the field subject:articles在字段subject上具有版本3text索引:
db.articles.createIndex( { subject: "text" } )
Populate the collection with the following documents:用以下文档填充集合:
db.articles.insertMany( [
{ _id: 1, subject: "coffee", author: "xyz", views: 50 },
{ _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
{ _id: 3, subject: "Baking a cake", author: "abc", views: 90 },
{ _id: 4, subject: "baking", author: "xyz", views: 100 },
{ _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
{ _id: 6, subject: "Сырники", author: "jkl", views: 80 },
{ _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
{ _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
] )
$text with a Single Word用一个单词
The following example specifies a 以下示例指定了$search string of coffee:coffee的$search字符串:
db.articles.find( { $text: { $search: "coffee" } } )
This operation returns the documents that contain the term 此操作返回索引coffee in the indexed subject field, or more precisely, the stemmed version of the word:subject字段中包含术语coffee的文档,或者更确切地说,返回单词的词干版本:
{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }
Match Any of the $search Terms匹配任何$search术语
$search TermsIf the 如果$search string is a space-delimited string, $text performs a logical OR operation on each term and returns documents that contain any of the terms.$search字符串是空格分隔的字符串,则$text对每个术语执行逻辑OR运算,并返回包含任何术语的文档。
The following example specifies a 以下示例指定了一个由空格分隔的三个术语组成的$search string of three terms delimited by space, "bake coffee cake":$search字符串"bake coffee cake":
db.articles.find( { $text: { $search: "bake coffee cake" } } )
This operation returns documents that contain either 此操作返回索引bake or coffee or cake in the indexed subject field, or more precisely, the stemmed version of these words:subject字段中包含bake、coffee或cake的文档,或者更确切地说,是这些单词的词干版本:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }
{ "_id" : 3, "subject" : "Baking a cake", "author" : "abc", "views" : 90 }
{ "_id" : 4, "subject" : "baking", "author" : "xyz", "views" : 100 }
$text with an Exact String带有精确字符串
To match an exact multi-word string as a single term, escape the quotes.要将精确的多单词字符串匹配为单个术语,请转义引号。
The following example matches the exact string 以下示例与字符串coffee shop:coffee shop完全匹配:
db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
This operation returns documents that contain the string 此操作返回包含字符串coffee shop:coffee shop的文档:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
The following example matches the strings 以下示例匹配字符串coffee shop and Cafe con Leche. This is a logical OR of the two strings.coffee shop和Cafe con Leche。这是两个字符串的逻辑OR。
db.articles.find( { $text: { $search: "\'coffee shop\' \'Cafe con Leche\'" } } )
This operation returns documents that contain both the strings, including documents that contain terms from both the strings:此操作返回同时包含这两个字符串的文档,包括包含两个字符串中的术语的文档:
[
{ _id: 8, subject: 'Cafe con Leche', author: 'xyz', views: 10 },
{ _id: 5, subject: 'Café Con Leche', author: 'abc', views: 200 },
{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }
]
Tip
Exclude Documents That Contain a Term排除包含术语的文档
A negated term is a term that is prefixed by a minus sign 否定项是以减号-. If you negate a term, the $text operator excludes the documents that contain those terms from the results.-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。
The following example matches documents that contain the word 以下示例匹配包含单词coffee but do not contain the term shop, or more precisely the stemmed version of the words:coffee但不包含单词shop的文档,或者更确切地说是单词的词干版本:
db.articles.find( { $text: { $search: "coffee -shop" } } )
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }
Query a Different Language查询其他语言
Use 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.$text表达式中的可选$language字段指定一种语言,该语言决定了$search字符串的停用词列表以及词干分析器和标记器的规则。
If you specify a 如果指定default_language value of none, the text index parses through each word in the field, including stop words, and ignores suffix stemming.default_language值none,文本索引将解析字段中的每个单词,包括停用词,并忽略后缀词干。
The following example specifies 以下示例指定es, i.e. Spanish, as the language that determines the tokenization, stemming, and stop words:es(即西班牙语)作为确定标记化、词干和停用词的语言:
db.articles.find(
{ $text: { $search: "leche", $language: "es" } }
)
The example returns the following documents:该示例返回以下文档:
{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }
The $text expression can also accept the language by name, spanish. See Text Search Languages on Self-Managed Deployments for the supported languages.$text表达式还可以按名称接受语言,spanish。有关支持的语言,请参阅自我管理部署上的文本搜索语言。
Case and Diacritic Insensitivity案例与Diacritic不敏感
The $text operator defers to the case and diacritic insensitivity of the text index. $text运算符遵循text索引对大小写和变音符号的不敏感性。The version 3 版本3的text index is diacritic insensitive and expands its case insensitivity to include the Cyrillic alphabet as well as characters with diacritics. text索引对变音符号不敏感,并将其不区分大小写的特性扩展到包括西里尔字母以及带有变音符号的字符。For details, see text Index Case Insensitivity and text Index Diacritic Insensitivity.有关详细信息,请参阅文本索引大小写不敏感和文本索引变音符号不敏感。
The following example performs a case and diacritic insensitive text query for the terms 以下示例对术语сы́рники or CAFÉS:сы́рники或CAFÉS执行不区分大小写和变音符号的文本查询:
db.articles.find( { $text: { $search: "сы́рники CAFÉS" } } )
Using the version 3 使用版本3text index, the operation matches the following documents.text索引,该操作将匹配以下文档。
{ "_id" : 6, "subject" : "Сырники", "author" : "jkl", "views" : 80 }
{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }
With previous versions of the 使用以前版本的text index, the query would not match any document.text索引,查询将不匹配任何文档。
Case Sensitivity大小写敏感
To enable case sensitivity, specify 要启用区分大小写,请指定$caseSensitive: true. Specifying $caseSensitive: true may impact performance.$caseSensitive: true。指定$caseSensitive: true可能会影响性能。
Case Sensitivity with a Term带有术语的案例敏感性
The following example performs a case sensitive query for the term 以下示例对术语Coffee:Coffee执行区分大小写的查询:
db.articles.find( { $text: { $search: "Coffee", $caseSensitive: true } } )
The operation matches just the following document:该操作仅匹配以下文档:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
Case Sensitivity with an Exact String使用精确字符串区分大小写
The following example performs a case sensitive query for the exact multi-word string 以下示例对确切的多单词字符串Café Con Leche:Café Con Leche执行区分大小写的查询:
db.articles.find( {
$text: { $search: "\"Café Con Leche\"", $caseSensitive: true }
} )
The operation matches just the following document:该操作仅匹配以下文档:
{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
Case Sensitivity with Negated Term带否定项的案例敏感性
A 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. You can also specify case sensitivity for negated terms.-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。您还可以为否定项指定区分大小写。
The following example performs a case sensitive query for documents that contain the word 以下示例对包含单词Coffee but do not contain the lower-case term shop, or more precisely the stemmed version of the words:Coffee但不包含小写单词shop的文档执行区分大小写的查询,或者更确切地说,不包含单词的词干版本:
db.articles.find( { $text: { $search: "Coffee -shop", $caseSensitive: true } } )
The operation matches the following document:该操作与以下文档匹配:
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg" }
Diacritic Sensitivity变音符号敏感
To enable diacritic sensitivity with a version 3 text index, specify 要使用版本3$diacriticSensitive: true. Specifying $diacriticSensitive: true may impact performance.text索引启用变音符号敏感性,请指定$diacriticSensitive: true。指定$diacriticSensitive: true可能会影响性能。
Diacritic Sensitivity with a Term带有术语的变音符号敏感性
The following example performs a diacritic sensitive text query on the term 以下示例对CAFÉ, or more precisely the stemmed version of the word:CAFÉ一词,或者更确切地说是该词的词干版本,执行对变音符号敏感的文本查询:
db.articles.find( { $text: { $search: "CAFÉ", $diacriticSensitive: true } } )
The operation only matches the following document:该操作仅匹配以下文档:
{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc" }
Diacritic Sensitivity with Negated Term否定条件下的辩证敏感性
The $diacriticSensitive option applies also to negated terms. A 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.$diacriticSensitive选项也适用于被否定的条款。否定项是以减号-为前缀的项。如果否定一个术语,$text运算符将从结果中排除包含这些术语的文档。
The following example performs a diacritic sensitive text query for documents that contain the term 以下示例对包含术语leches but not the term cafés, or more precisely the stemmed version of the words:leches但不包含术语cafés的文档执行对变音符号敏感的文本查询,或者更确切地说,是单词的词干版本:
db.articles.find(
{ $text: { $search: "leches -cafés", $diacriticSensitive: true } }
)
The operation matches the following document:该操作与以下文档匹配:
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz" }
Relevance Score Examples相关性得分示例
Return the Relevance Score返回相关性得分
The following example performs a text query for the term 以下示例对术语cake and uses the $meta operator in the projection document to append the relevance score to each matching document:cake执行文本查询,并在投影文档中使用$meta运算符将相关性得分附加到每个匹配的文档中:
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
)
The returned document includes an additional field 返回的文档包括一个额外的字段score that contains the document's relevance score:score,其中包含文档的相关性分数:
{ "_id" : 3, "subject" : "Baking a cake", "author" : "abc", "views" : 90, "score" : 0.75 }
Tip
Sort by Relevance Score按相关性得分排序
You can specify the您可以在{ $meta: "textScore" }expression in thesort()without also specifying the expression in the projection. For example:sort()中指定{ $meta: "textScore" }表达式,而无需在投影中指定表达式。例如:db.articles.find(
{ $text: { $search: "cake" } }
).sort( { score: { $meta: "textScore" } } )As a result, you can sort the resulting documents by their relevance without projecting the因此,您可以按相关性对生成的文档进行排序,而无需投影textScore.textScore。If you include the如果在投影和{ $meta: "textScore" }expression in both the projection andsort(), the projection and sort documents can have different field names for the expression.sort()中都包含{ $meta: "textScore" }表达式,则投影和排序文档可以为表达式使用不同的字段名。For example, in the following operation, the projection uses a field named例如,在以下操作中,投影使用名为scorefor the expression and thesort()uses the field namedignoredName.score的字段作为表达式,sort()使用名为ignoredName的字段。db.articles.find(
{ $text: { $search: "cake" } } ,
{ score: { $meta: "textScore" } }
).sort( { ignoredName: { $meta: "textScore" } } )
Tip
Return Top 2 Matching Documents返回前2个匹配文档
Use the 将limit() method in conjunction with a sort() to return the top n matching documents.limit()方法与sort()结合使用,返回前n个匹配的文档。
The following example queries for the term 以下示例查询术语coffee and sorts the results by the descending score, limiting the results to the top two matching documents:coffee,并按降序对结果进行排序,将结果限制在前两个匹配的文档中:
db.articles.find(
{ $text: { $search: "coffee" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } ).limit(2)
Tip
$text with Additional Query and Sort Expressions带有附加查询和排序表达式的$text
The following example matches documents where the 以下示例匹配author equals "xyz" and the indexed field subject contains the terms coffee or bake. The operation also specifies a sort order of ascending date, then descending relevance score:author等于"xyz"且索引字段subject题包含术语coffee或bake的文档。该操作还指定了date升序、相关性得分降序的排序顺序:
db.articles.find(
{ author: "xyz", $text: { $search: "coffee bake" } },
{ score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )