On this page本页内容
MongoDB Atlas SearchMongoDB Atlas搜索
Atlas Search makes it easy to build fast, relevance-based search capabilities on top of your MongoDB data. Atlas Search可以轻松地在MongoDB数据的基础上构建快速、基于相关性的搜索功能。Try it today on MongoDB Atlas, our fully managed database as a service.今天就在MongoDB Atlas上试试吧,这是我们全面管理的数据库即服务。
$text
¶$text
performs a text search on the content of the fields indexed with a text index. 对使用文本索引索引的字段的内容执行文本搜索。A $text
expression has the following syntax:$text
语法如下所示:
Changed in version 3.2.在版本3.2中更改。
The $text
operator accepts a text query document with the following fields:$text
运算符接受包含以下字段的文本查询文档:
$search |
string | OR search of the terms unless specified as a phrase. OR 搜索,除非指定为短语。 |
$language |
string |
|
$caseSensitive |
boolean |
|
$diacriticSensitive |
boolean |
|
The 默认情况下,$text
operator, by default, does not return results sorted in terms of the results’ scores. $text
运算符不返回按结果分数排序的结果。For more information on sorting by the text search scores, see the Text Score documentation.有关按文本搜索分数排序的更多信息,请参阅文本分数文档。
$text
expression.$text
表达式。$text
query can not appear in $nor
expressions.$text
查询不能出现在$nor
表达式中。$text
query can not appear in $elemMatch
query expressions or $elemMatch
projection expressions.$text
查询不能出现在$elemMatch
查询表达式或$elemMatch
投影表达式中。$text
query in an $or
expression, all clauses in the $or
array must be indexed.$or
表达式中使用$text
查询,必须为$or
数组中的所有子句编制索引。hint()
if the query includes a $text
query expression.$text
查询表达式,则不能使用hint()
。$natural
sort order if the query includes a $text
expression.$text
表达式,则不能指定$natural
排序顺序。$text
expression, which requires a special text index, with a query operator that requires a different type of special index. $text
表达式与需要不同类型特殊索引的查询运算符组合使用。$text
expression with the $near
operator.$text
表达式与$near
运算符组合使用。If using the 如果在聚合中使用$text
operator in aggregation, the following restrictions also apply.$text
运算符,则以下限制也适用。
$match
stage that includes a $text
must be the first stage in the pipeline.$text
的$match
阶段必须是管道中的第一个阶段。text
operator can only occur once in the stage.text
运算符只能在阶段中出现一次。text
operator expression cannot appear in $or
or $not
expressions.text
运算符表达式不能出现在$or
或$not
表达式中。$meta
aggregation expression in the $sort
stage.$sort
阶段使用$meta
聚合表达式。$search
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 a phrase.text
运算符将字符串中的大多数标点符号视为分隔符,但用于否定术语的连字符减号(-
)或用于指定短语的转义双引号\"
除外。
To match on a phrase, as opposed to individual terms, enclose the phrase in escaped double quotes (要匹配短语,而不是单个术语,请将短语括在转义双引号(\"
), as in:\"
)中,如下所示:
If the 如果$search
string includes a phrase and individual terms, text search will only match the documents that include the phrase.$search
字符串包含短语和单个术语,则文本搜索将仅匹配包含该短语的文档。
For example, passed a 例如,传递了$search
string:$search
字符串:
The $text
operator searches for the phrase "ssl certificate"
.$text
运算符搜索短语"ssl certificate"
。
Prefixing a word with a hyphen-minus (在单词前面加上连字符减号(-
) negates a word:-
)将否定单词:
pre-market
, is not a negation. pre-market
)不是否定词。$text
operator treats the hyphen-minus (-
) as a delimiter. $text
运算符将连字符减号(-)视为分隔符。market
in this instance, include a space between pre
and -market
, i.e., pre -market
.market
,请在pre
和-market
之间加一个空格,即pre -market
。The $text
operator adds all negations to the query with the logical AND
operator.$text
运算符使用逻辑AND
运算符将所有否定添加到查询中。
The $text
operator ignores language-specific stop words, such as the
and and
in English.$text
运算符忽略特定语言的停止词,例如英语中的the
和and
。
For case insensitive and diacritic insensitive text searches, the 对于不区分大小写和不区分重音的文本搜索,$text
operator matches on the complete stemmed word. $text
运算符匹配完整的词干单词。So if a document field contains the word 因此,如果文档字段包含blueberry
, a search on the term blue
will not match. blueberry
一词,则对blue
一词的搜索将不匹配。However, 然而,blueberry
or blueberries
will match.blueberry
或blueberrys
会相配。
For case sensitive search (i.e. 对于区分大小写的搜索(即$caseSensitive: true
), if the suffix stem contains uppercase letters, the $text
operator matches on the exact word.$caseSensitive: true
),如果后缀词干包含大写字母,则$text
运算符将匹配确切的单词。
For diacritic sensitive search (i.e. 对于区分变音符号的搜索(即$diacriticSensitive: true
), if the suffix stem contains the diacritic mark or marks, the $text
operator matches on the exact word.$diacriticSensitive: true
),如果后缀词干包含一个或多个变音符号,则$text
运算符将匹配准确的单词。
Changed in version 3.2.已在版本3.2中更改。
The $text
operator defaults to the case insensitivity of the text index:$text
运算符默认为文本索引的大小写不敏感:
text
index are case insensitive for Latin characters without diacritic marks; i.e. for [A-z]
.text
索引对没有变音符号的拉丁字符不区分大小写;例如,对于[A-z]
。$caseSensitive
To support case sensitive search where the 若要在text
index is case insensitive, specify $caseSensitive: true
.text
索引不区分大小写的情况下支持区分大小写的搜索,请指定$caseSensitive:true
。
When performing a case sensitive search (在$caseSensitive: true
) where the text
index is case insensitive, the $text
operator:text
索引不区分大小写的情况下执行区分大小写的搜索($caseSensitive: true
)时,$text
运算符:
text
index for case insensitive and diacritic matches.text
索引中搜索不区分大小写和区分重音的匹配项。$text
query operation includes an additional stage to filter out the documents that do not match the specified case.$text
查询操作还包括一个额外的阶段,以筛选出与指定大小写不匹配的文档。For case sensitive search (i.e. 对于区分大小写的搜索(即$caseSensitive: true
), 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
可能会影响性能。
See also参阅
Changed in version 3.2.已在版本3.2中更改。
The $text
operator defaults to the diacritic insensitivity of the text index:$text
运算符默认为text索引的不区分重音:
é
, ê
, and e
.é
、ê
和e
。text
index are diacritic sensitive.text
索引区分重音。$diacriticSensitive
To support diacritic sensitive text search against the version 3 要支持对版本3文本索引进行区分重音的text
index, specify $diacriticSensitive: true
.text
搜索,请指定$diacriticSensitive:true
。
Text searches against earlier versions of the 针对早期版本的text
index are inherently diacritic sensitive and cannot be diacritic insensitive. text
索引的文本搜索本质上是区分重音的,不能不区分重音。As such, the 因此,$diacriticSensitive
option for the $text
operator has no effect with earlier versions of the text
index.$text
运算符的$diacriticSensitive
选项对早期版本的text
索引没有影响。
To perform a diacritic sensitive text search (要对版本3文本索引执行区分重音的$diacriticSensitive: true
) against a version 3 text
index, the $text
operator:text
搜索($diacriticSensitive : true
),请使用$text
运算符:
text
index, which is diacritic insensitive.text
索引。$text
query operation includes an additional stage to filter out the documents that do not match.$text
查询操作还包括一个额外的阶段来筛选不匹配的文档。Specifying 指定$diacriticSensitive: true
may impact performance.$diacriticSensitive :true
可能会影响性能。
To perform a diacritic sensitive search against an earlier version of the 要对早期版本的text
index, the $text
operator searches the text
index which is diacritic sensitive.text
索引执行区分重音的搜索,$text
运算符将搜索区分重音的text
索引。
For diacritic sensitive search, if the suffix stem contains the diacritic mark or marks, the 对于区分变音符号的搜索,如果后缀词干包含一个或多个变音符号,则$text
operator matches on the exact word.$text
运算符将匹配确切的单词。
See also参阅
The $text
operator assigns a score to each document that contains the search term in the indexed fields. $text
运算符为索引字段中包含搜索项的每个文档分配一个分数。The score represents the relevance of a document to a given text search query. 分数表示文档与给定文本搜索查询的相关性。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
投影运算符。
The following examples assume a collection 以下示例假定某个集合articles
that has a version 3 text index on the field subject
:articles
在字段subject
上具有版本3文本索引:
Populate the collection with the following documents:使用以下文档填充集合:
The following query specifies a 以下查询指定了一个$search
string of coffee
:$search
字符串coffee
:
This query returns the documents that contain the term 此查询返回索引coffee
in the indexed subject
field, or more precisely, the stemmed version of the word:subject
字段中包含术语coffee
的文档,或者更准确地说,返回单词的词干版本:
See also参阅
If 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.$text
运算符对每个术语执行逻辑OR
搜索,并返回包含任何术语的文档。
The following query specifies a 下面的查询指定了一个$search
string of three terms delimited by space, "bake coffee cake"
:$search
字符串,由三个由空格分隔的词组成,"bake coffee cake"
:
This query 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
的文档,或者更准确地说,返回这些单词的词干版本:
See also参阅
To match the exact phrase as a single term, escape the quotes.要将准确的短语作为一个术语进行匹配,请跳过引号。
The following query searches for the phrase 以下查询将搜索短语coffee shop
:coffee shop
:
This query returns documents that contain the phrase 此查询返回包含短语coffee shop
:coffee shop
的文档:
See also参阅
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.$text
运算符将从结果中排除包含这些术语的文档。
The following example searches for documents that contain the words 以下示例搜索包含单词coffee
but do not contain the term shop
, or more precisely the stemmed version of the words:coffee
但不包含术语shop
的文档,或者更准确地说,搜索单词的词干版本:
The query returns the following documents:查询返回以下文档:
See also参阅
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
字段指定一种语言,该语言确定停止词列表以及搜索字符串的词干分析器和标记器规则。
If you specify a language value of 如果指定语言值"none"
, then the text search uses simple tokenization with no list of stop words and no stemming."none"
,则文本搜索使用简单的标记化,没有停止词列表,也没有词干。
The following query specifies 以下查询指定es
, i.e. Spanish, as the language that determines the tokenization, stemming, and stop words:es
(即西班牙语)作为确定标记化、词干和停止词的语言:
The query returns the following documents:查询返回以下文档:
The $text
expression can also accept the language by name, spanish
. $text
表达式也可以接受名称为spanish
的语言。See Text Search Languages for the supported languages.有关支持的语言,请参阅文本搜索语言。
See also参阅
Changed in version 3.2.已在版本3.2中更改。
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 query performs a case and diacritic insensitive text search for the terms 以下查询对术语сы́рники
or CAFÉS
:сы́рники
或CAFÉS
执行大小写和不区分重音的文本搜索:
Using the version 3 使用版本3text
index, the query matches the following documents.text
索引,查询将匹配以下文档。
With the previous versions of the 对于以前版本的text
index, the query would not match any document.text
索引,查询将与任何文档都不匹配。
Changed in version 3.2.已在版本3.2中更改。
To enable case sensitive search, specify 要启用区分大小写的搜索,请指定$caseSensitive: true
. $caseSensitive : true
。Specifying 指定$caseSensitive: true
may impact performance.$caseSensitive : true
可能会影响性能。
The following query performs a case sensitive search for the term 以下查询对术语Coffee
:Coffee
执行区分大小写的搜索:
The search matches just the document:搜索仅与文档匹配:
The following query performs a case sensitive search for the phrase 以下查询对短语Café Con Leche
:Café Con Leche
执行区分大小写的搜索:
The search matches just the document:搜索仅与文档匹配:
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. $text
运算符将从结果中排除包含这些术语的文档。You can also specify case sensitivity for negated terms.还可以为否定项指定大小写敏感度。
The following example performs a case sensitive search 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
的文档执行区分大小写的搜索,更确切地说,不包含单词的词干版本:
The query matches the following document:查询与以下文档匹配:
Changed in version 3.2.已在版本3.2中更改。
To enable diacritic sensitive search against a version 3 text index, specify 要对版本3text索引启用区分重音的搜索,请指定$diacriticSensitive: true
. $diacriticSensitive : true
。Specifying 指定$diacriticSensitive: true
may impact performance.$diacriticSensitive : true
可能会影响性能。
The following query performs a diacritic sensitive text search on the term 以下查询对术语CAFÉ
, or more precisely the stemmed version of the word:CAFÉ
或更准确地说是单词的词干版本执行区分重音的文本搜索:
The query only matches the following document:查询仅与以下文档匹配:
The $diacriticSensitive
option applies also to negated terms. $diacriticSensitive
选项也适用于否定条款。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.$text
运算符将从结果中排除包含这些术语的文档。
The following query performs a diacritic sensitive text search for document that contains the term 以下查询对包含术语leches
but not the term cafés
, or more precisely the stemmed version of the words:leches
但不包含术语cafés
的文档执行区分重音的文本搜索,或者更准确地说,搜索词干版本的文档:
The query matches the following document:查询与以下文档匹配:
The following query performs a text search for the term 以下查询对术语cake
and uses the $meta
operator in the projection document to append the relevance score to each matching document:cake
执行文本搜索,并在投影文档中使用$meta
运算符将相关性得分附加到每个匹配文档中:
The returned document includes an additional field 返回的文档包括一个额外的字段score
that contains the document’s relevance score:score
,该分数包含文档的相关性分数:
See also参阅
{ $meta: "textScore" }
expression in the sort()
without also specifying the expression in the projection. sort()
中指定{ $meta: "textScore" }
表达式,而无需在投影中指定表达式。As a result, you can sort the resulting documents by their search relevance without projecting the 因此,您可以根据搜索相关性对结果文档进行排序,而无需投影textScore
.textScore
。
{ $meta: "textScore" }
expression in the sort()
, you must also include the same expression in the projection.sort()
中包含{ $meta: "textScore" }
表达式,还必须在投影中包含相同的表达式。{ $meta: "textScore" }
expression in both the projection and sort()
, the projection and sort documents can have different field names for the expression.sort()
中都包含{ $meta: "textScore" }
表达式,那么投影文档和排序文档可以有不同的表达式字段名。
In previous versions of MongoDB, if 在MongoDB的早期版本中,如果投影和排序中都包含{ $meta: "textScore" }
is included in both the projection and sort, you must specify the same field name for the expression.{ $meta: "textScore" }
,则必须为表达式指定相同的字段名。
$meta
expression in both the projection document and the sort expression. $meta
表达式。coffee
and sorts the results by the descending score:coffee
一词,并按递减分数对结果进行排序:
The query returns the matching documents sorted by descending score.查询返回按分数降序排序的匹配文档。
See also参阅
Use the 结合使用limit()
method in conjunction with a sort()
to return the top n
matching documents.limit()
方法和sort()
返回前n
个匹配文档。
The following query searches for the term 以下查询将搜索coffee
and sorts the results by the descending score, limiting the results to the top two matching documents:coffee
一词,并按递减分数对结果进行排序,将结果限制在前两个匹配的文档中:
See also参阅
The following query searches for documents where the 下面的查询搜索author
equals "xyz"
and the indexed field subject
contains the terms coffee
or bake
. author
等于"xyz"
且索引字段subject
包含术语coffee
或bake
的文档。The operation also specifies a sort order of ascending 该操作还指定了date
, then descending text search score:date
升序、文本搜索分数降序的排序顺序: