On this page本页内容
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则。
You can specify collation for a collection or a view, an index, or specific operations that support collation.可以为集合或视图、索引或支持排序的特定操作指定排序规则。
A collation document has the following fields:排序规则文档具有以下字段:
{
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.有关这些字段的描述,请参阅排序规则文档。
Default collation parameter values vary depending on which locale you specify. 默认排序规则参数值因指定的区域设置而异。For a complete list of default collation parameters and the locales they are associated with, see Collation Default Parameters.有关默认排序规则参数及其关联的地区的完整列表,请参阅排序规则默认参数。
locale | string |
| ||||||||||||
strength | integer |
| ||||||||||||
caseLevel | boolean |
| ||||||||||||
caseFirst | string |
| ||||||||||||
numericOrdering | boolean |
Default is
| ||||||||||||
alternate | string |
| ||||||||||||
maxVariable | string |
| ||||||||||||
backwards | boolean |
| ||||||||||||
normalization | boolean |
|
You can specify collation for the following operations:可以为以下操作指定排序规则:
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.例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能对查找使用一种排序规则,对排序使用另一种排序规则。
mongosh | |
|---|---|
create | |
createIndexes [1] | db.collection.createIndex() [1] |
aggregate | db.collection.aggregate() |
distinct | db.collection.distinct() |
findAndModify | |
find | cursor.collation()db.collection.find()db.collection.find()的排序规则 |
mapReduce | db.collection.mapReduce() |
delete | |
update | |
shardCollection | |
count | |
db.collection.bulkWrite().db.collection.bulkWrite()中的单个更新、替换和删除操作。 |
| [1] | (1, 2) |
Some collation locales have variants, which employ special language-specific rules. 一些排序规则区域设置有变体,它们使用特定于语言的特殊规则。To specify a locale variant, use the following syntax:要指定区域设置变量,请使用以下语法:
{ "locale" : "<locale code>@collation=<variant>" }
For example, to use the 例如,要使用中文排序规则的unihan variant of the Chinese collation:unihan变体:
{ "locale" : "zh@collation=unihan" }
For a complete list of all collation locales and their variants, see Collation Locales.有关所有排序规则区域设置及其变体的完整列表,请参阅排序规则区域设置。
$lookup or $graphLookup, the views must have the same collation.$lookup或$graphLookup,则这些视图必须具有相同的排序规则。To use an index for string comparisons, an operation must also specify the same collation. 要使用索引进行字符串比较,操作还必须指定相同的排序规则。That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation.也就是说,如果操作指定了不同的排序规则,则具有排序规则的索引不能支持对索引字段执行字符串比较的操作。
For example, the collection 例如,集合myColl has an index on a string field category with the collation locale "fr".myColl在排序规则区域设置为"fr"的字符串字段category上有一个索引。
db.myColl.createIndex( { category: 1 }, { collation: { locale: "fr" } } )
The following query operation, which specifies the same collation as the index, can use the index:以下查询操作指定了与索引相同的排序规则,可以使用索引:
db.myColl.find( { category: "cafe" } ).collation( { locale: "fr" } )
However, the following query operation, which by default uses the "simple" binary collator, cannot use the index:但是,以下查询操作(默认情况下使用“简单”二进制排序器)不能使用索引:
db.myColl.find( { category: "cafe" } )
For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys.对于索引前缀键不是字符串、数组和嵌入文档的复合索引,指定不同排序规则的操作仍然可以使用索引来支持索引前缀键的比较。
For example, the collection 例如,集合myColl has a compound index on the numeric fields score and price and the string field category; the index is created with the collation locale "fr" for string comparisons:myColl在数字字段score和price以及字符串字段category上有一个复合索引;索引是使用排序规则语言环境"fr"创建的,用于字符串比较:
db.myColl.createIndex(
{ score: 1, price: 1, category: 1 },
{ collation: { locale: "fr" } } )
The following operations, which use 以下操作使用"simple" binary collation for string comparisons, can use the index:"simple"二进制排序规则进行字符串比较,可以使用索引:
db.myColl.find( { score: 5 } ).sort( { price: 1 } )
db.myColl.find( { score: 5, price: { $gt: NumberDecimal( "10" ) } } ).sort( { price: 1 } )
The following operation, which uses 以下操作使用"simple" binary collation for string comparisons on the indexed category field, can use the index to fulfill only the score: 5 portion of the query:"simple"二进制排序规则对索引category字段进行字符串比较,可以使用索引仅完成查询的score: 5部分:
db.myColl.find( { score: 5, category: "cafe" } )
The following indexes only support simple binary comparison and do not support collation:以下索引仅支持简单的二进制比较,不支持排序规则:
To create a 要在具有非简单排序规则的集合上创建text, a 2d, or a geoHaystack index on a collection that has a non-simple collation, you must explicitly specify {collation: {locale: "simple"} } when creating the index.text索引、2d索引或geoHaystack索引,必须在创建索引时显式指定{collation: {locale: "simple"} }。
When specifying the 将numericOrdering as true the following restrictions apply:numericOrdering指定为true时,以下限制适用:
Only contiguous non-negative integer substrings of digits are considered in the comparisons.在比较中只考虑连续的非负整数子串。
numericOrdering does not support:不支持:
+-Consider a collection with the following string number and decimal values:考虑具有以下字符串数和十进制值的集合:
db.c.insertMany(
[
{ "n" : "1" },
{ "n" : "2" },
{ "n" : "2.1" },
{ "n" : "-2.1" },
{ "n" : "2.2" },
{ "n" : "2.10" },
{ "n" : "2.20" },
{ "n" : "-10" },
{ "n" : "10" },
{ "n" : "20" },
{ "n" : "20.1" }
]
)
The following 以下find query uses a collation document containing the numericOrdering parameter:find查询使用包含numericOrdering参数的排序规则文档:
db.c.find(
{ }, { _id: 0 }
).sort(
{ n: 1 }
).collation( {
locale: 'en_US',
numericOrdering: true
} )
The operation returns the following results:该操作返回以下结果:
[
{ n: '-2.1' },
{ n: '-10' },
{ n: '1' },
{ n: '2' },
{ n: '2.1' },
{ n: '2.2' },
{ n: '2.10' },
{ n: '2.20' },
{ n: '10' },
{ n: '20' },
{ n: '20.1' }
]
numericOrdering: true-2.1 and -10 are not sorted in the expected sort order because they have unsupported - characters.-2.1和-10没有按预期的排序顺序排序,因为它们有不支持的-字符。2.2 is sorted before the value 2.10, due to the fact that the numericOrdering parameter does not support decimal values.numericOrdering参数不支持十进制值,因此值2.2排序在值2.10之前。2.2 and 2.10 are sorted in lexicographic order.2.2和2.10是按字典顺序排序的。