cursor.collation()
On this page本页内容
Definition定义
cursor.collation(<collation document>)
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for a language-specific driver, such as Node.js.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Specifies the collation for the cursor returned by the指定db.collection.find()
.db.collection.find()
返回的游标的排序规则。To use, append to the若要使用,请附加到db.collection.find()
.db.collection.find()
。Thecursor.collation()
accepts the following collation document:cursor.collation()
接受以下排序规则文档:{
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}When specifying collation, the指定排序规则时,locale
field is mandatory; all other collation fields are optional.locale
字段是必需的;所有其他排序规则字段都是可选的。For descriptions of the fields, see Collation Document.有关字段的说明,请参阅排序规则文档。Field字段Type类型Description描述locale
string The ICU locale.ICU所在地。See Supported Languages and Locales for a list of supported locales.有关支持的区域设置的列表,请参阅支持的语言和区域设置。
To specify simple binary comparison, specify若要指定简单的二进制比较,请指定locale
value of"simple"
.locale
设置值"simple"
。strength
integer Optional.可选的。The level of comparison to perform.要执行的比较级别。Corresponds to ICU Comparison Levels对应ICU比较级别. Possible values are:
。可能的值为:
Value值Description描述1 Primary level of comparison.初级比较。Collation performs comparisons of the base characters only, ignoring other differences such as diacritics and case.排序规则只执行基本字符的比较,忽略其他差异,如变音符号和大小写。2 Secondary level of comparison.第二级比较。Collation performs comparisons up to secondary differences, such as diacritics. That is, collation performs comparisons of base characters (primary differences) and diacritics (secondary differences).排序执行比较,直到次要差异,例如变音符号。也就是说,排序规则执行基本字符(主要差异)和变音符号(次要差异)的比较。Differences between base characters takes precedence over secondary differences.基本字符之间的差异优先于次要差异。3 Tertiary level of comparison.第三级比较。Collation performs comparisons up to tertiary differences, such as case and letter variants.排序规则执行比较,直到第三级差异,例如大小写和字母变体。That is, collation performs comparisons of base characters (primary differences), diacritics (secondary differences), and case and variants (tertiary differences).也就是说,排序规则执行基本字符(主要差异)、变音符号(次要差异)以及大小写和变体(第三差异)的比较。Differences between base characters takes precedence over secondary differences, which takes precedence over tertiary differences.基本字符之间的差异优先于二级差异,二级差异优先于三级差异。
This is the default level.这是默认级别。4 Quaternary Level.第四级比较。Limited for specific use case to consider punctuation when levels 1-3 ignore punctuation or for processing Japanese text.当1-3级忽略标点符号或用于处理日语文本时,仅限于考虑标点符号的特定用例。5 Identical Level. Limited for specific use case of tie breaker.相同级别。限用于断路器的特定使用情况。See ICU Collation: Comparison Levels有关详细信息,请参阅ICU排序:比较级别for details.
。
caseLevel
boolean Optional.可选的。Flag that determines whether to include case comparison at用于确定是在strength
level1
or2
.strength
级别1
还是在strength
级别2
包括事例比较的标志。
If如果为true
, include case comparison; i.e.true
,则包括案例比较;即。When used with当与strength:1
, collation compares base characters and case.strength:1
一起使用时,排序规则比较基本字符和大小写。When used with当与strength:2
, collation compares base characters, diacritics (and possible other secondary differences) and case.strength:2
一起使用时,排序规则比较基本字符、变音符号(以及可能的其他次要差异)和大小写。
If如果为false
, do not include case comparison at level1
or2
.false
,则不包括级别1
或级别2
的案例比较。The default is默认值为false
.false
。
For more information, see ICU Collation: Case Level有关详细信息,请参阅ICU排序:大小写级别.
。
caseFirst
string Optional.可选的。A field that determines sort order of case differences during tertiary level comparisons.一个字段,用于确定三级比较期间事例差异的排序顺序。
Possible values are:可能的值为:Value值Description描述"upper" Uppercase sorts before lowercase.大写在小写之前排序。"lower" Lowercase sorts before uppercase.小写排序在大写之前。"off" Default value. Similar to默认值。类似于"lower"
with slight differences."lower"
,略有差异。See http://userguide.icu-project.org/collation/customization请参阅http://userguide.icu-project.org/collation/customizationfor details of differences.
了解差异的详细信息。
numericOrdering
boolean Optional.可选的。Flag that determines whether to compare numeric strings as numbers or as strings.用于确定将数字字符串作为数字还是字符串进行比较的标志。
If如果为true
, compare as numbers; i.e."10"
is greater than"2"
.true
,则以数字形式进行比较;即"10"
大于"2"
。
If如果为false
, compare as strings; i.e."10"
is less than"2"
.false
,则作为字符串进行比较;即"10"
小于"2"
。
Default is默认值为false
.false
。alternate
string Optional.可选的。Field that determines whether collation should consider whitespace and punctuation as base characters for purposes of comparison.字段,用于确定排序规则是否应将空白和标点符号作为比较的基础字符。
Possible values are:可能的值为:Value值Description描述"non-ignorable"
Whitespace and punctuation are considered base characters.空格和标点符号被视为基本字符。"shifted"
Whitespace and punctuation are not considered base characters and are only distinguished at strength levels greater than 3.空白和标点符号不被视为基本字符,仅在强度等级大于3时才进行区分。See ICU Collation: Comparison Levels有关详细信息,请参阅ICU排序:比较级别for more information.
。
Default is默认值为"non-ignorable"
."non-ignorable"
。maxVariable
string Optional.可选的。Field that determines up to which characters are considered ignorable when字段,用于确定在alternate: "shifted"
. Has no effect ifalternate: "non-ignorable"
alternate: "shifted"
时最多可忽略哪些字符。如果alternate: "non-ignorable"
,则无效
Possible values are:可能的值为:Value值Description描述"punct"
Both whitespaces and punctuation are "ignorable", i.e. not considered base characters.空格和标点符号都是“可忽略的”,即不被视为基本字符。"space"
Whitespace are "ignorable", i.e. not considered base characters.空白是“可忽略的”,即不被视为基本字符。backwards
boolean Optional.可选的。Flag that determines whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.用于确定带变音符号的字符串是否从字符串后面排序的标志,例如使用某些法语字典排序。
If如果为真,则从后向前进行比较。true
, compare from back to front.
If如果为false
, compare from front to back.false
,则从前到后进行比较。
The default value is默认值为false
.false
。normalization
boolean Optional.可选的。Flag that determines whether to check if text require normalization and to perform normalization.用于确定是否检查文本是否需要规范化并执行规范化的标志。Generally, majority of text does not require this normalization processing.通常,大多数文本不需要这种规范化处理。
If如果为true
, check if fully normalized and perform normalization to compare text.true
,请检查是否已完全规范化并执行规范化以比较文本。
If如果为false
, does not check.false
,则不进行检查。
The default value is默认值为false
.false
。
See http://userguide.icu-project.org/collation/concepts#TOC-Normalizationfor details.
Examples实例
Consider a collection 考虑一个包含以下文档的集合foo
with the following documents:foo
:
{ "_id" : 1, "x" : "a" }
{ "_id" : 2, "x" : "A" }
{ "_id" : 3, "x" : "á" }
The following operation specifies a query filter of 以下操作指定了x: "a"
. x: "a"
的查询筛选器。The operation also includes a collation option with 该操作还包括一个locale: "en_US"
(US English locale) and strength: 1
(compare base characters only; i.e. ignore case and diacritics):locale: "en_US"
(美国英语区域设置)、strength: 1
(仅比较基本字符;即忽略大小写和变音符号)的排序规则选项:
db.foo.find( { x: "a" } ).collation( { locale: "en_US", strength: 1 } )
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "x" : "a" }
{ "_id" : 2, "x" : "A" }
{ "_id" : 3, "x" : "á" }
If you do not specify the collation, i.e. 如果不指定排序规则,即db.collection.find( { x: "a" } )
, the query would only match the following document:db.collection.find( { x: "a" } )
,则查询将仅与以下文档匹配:
db.foo.find( { x: "a" } )
You can chain other cursor methods, such as 您可以将其他游标方法(如cursor.sort()
and cursor.count()
, to cursor.collation()
:cursor.sort()
和cursor.count()
)链接到cursor.collation()
:
db.collection.find({...}).collation({...}).sort({...});
db.collection.find({...}).collation({...}).count();
You cannot specify multiple collations for an operation. 不能为一个操作指定多个排序规则。For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能为查找使用一个排序规则,为排序使用另一个排序顺序。