Collations排序规则¶
On this page
Collations are available in MongoDB 3.4 and later.MongoDB 3.4及更高版本中提供了排序规则。
Overview概述¶
This guide shows you how to use collations, a set of sorting rules, to run operations using string ordering for specific languages and locales (a community or region that shares common language idioms).本指南介绍如何使用排序规则(一组排序规则)对特定语言和地区(共享公共语言习惯用法的社区或地区)使用字符串排序来运行操作。
MongoDB sorts strings using binary collation by default. 默认情况下,MongoDB使用二进制排序规则对字符串进行排序。This collation method uses the ASCII standard character values to compare and order strings. 此排序方法使用ASCII标准字符值对字符串进行比较和排序。Languages and locales have specific character ordering conventions that differ from the ASCII standard.语言和地区具有不同于ASCII标准的特定字符排序约定。
For example, in Canadian French, the right-most accented character determines the ordering for strings when the other characters are the same. 例如,在加拿大法语中,当其他字符相同时,最右边的重音字符决定字符串的顺序。Consider the following French words: cote, coté, côte, and côté.请考虑以下法语单词:cote、coté、côte和côté。
MongoDB sorts them in the following order using the default binary collation:MongoDB使用默认的二进制排序规则按以下顺序对它们进行排序:
cote
coté
côte
côté
MongoDB sorts them in the following order using the Canadian French collation:MongoDB使用加拿大法语排序规则按以下顺序对其进行排序:
cote
côte
coté
côté
Usage用法¶
You can specify a collation when you create a new collection or new index. 创建新集合或新索引时,可以指定排序规则。You can also specify a collation for CRUD operations and aggregations.还可以为CRUD操作和聚合指定排序规则。
When you create a new collection with a collation, you define the default collation for any of the operations that support collation called on that collection. 使用排序规则创建新集合时,可以为支持对该集合调用的排序规则的任何操作定义默认排序规则。You can override the collation for an operation by specifying a different one.您可以通过指定其他排序规则来覆盖操作的排序规则。
Currently, you cannot create a collation on an existing collection. 当前,无法在现有集合上创建排序规则。To use collations with an existing collection, create an index with the collation and specify the same collation in your operations on it.若要对现有集合使用排序规则,请使用排序规则创建索引,并在对其执行的操作中指定相同的排序规则。
When you create an index with a collation, you specify the sort order for operations that use that index. 创建具有排序规则的索引时,可以为使用该索引的操作指定排序顺序。To use the collation in the index, you must provide a matching collation in the operation, and the operation must use the index. 要在索引中使用排序规则,必须在操作中提供匹配的排序规则,并且操作必须使用索引。While most index types support collation, the following types support only binary comparison:虽然大多数索引类型支持排序规则,但以下类型仅支持二进制比较:
Collation Parameters排序规则参数¶
The collation object contains the following parameters:排序规则对象包含以下参数:
collation: {
locale: <string>,
caseLevel: <bool>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <bool>,
alternate: <string>,
maxVariable: <string>,
backwards: <bool>
}
You must specify the 必须在排序规则中指定locale
field in the collation; all other fields are optional. locale
设置字段;所有其他字段都是可选的。For a complete list of supported locales and the default values for the 有关支持的区域设置的完整列表和locale
fields, see Supported Languages and Locales. locale
字段的默认值,请参阅支持的语言和区域设置。For descriptions of each field, see the Collation Document MongoDB manual entry.有关每个字段的说明,请参阅排序规则文档MongoDB手册条目。
Collation Examples整理示例¶
Set a Default Collation on a Collection设置集合的默认排序规则¶
In the following example, we create a new collection called 在下面的示例中,我们创建了一个名为souvenirs
and assign a default collation with the "fr_CA
" locale. souvenirs
(纪念品)的新集合,并使用“fr_CA
”(加拿大法语)语言环境指定了一个默认排序规则。The collation applies to all operations that support collation performed on that collection.排序规则适用于支持对该集合执行排序规则的所有操作。
// Create the collection with a collation使用排序规则创建集合
db.createCollection("souvenirs", {
collation: { locale: "fr_CA" },
});
Any of the operations that support collations automatically apply the collation defined on the collection. 支持排序规则的任何操作都会自动应用集合上定义的排序规则。The query below searches the 以下查询搜索souvenirs
collection and applies the "fr_CA
" locale collation:souvenirs
集合并应用“fr_CA”区域设置排序规则:
collection.find({type: "photograph"});
You can specify a different collation as a parameter in an operation that supports collations. 您可以在支持排序规则的操作中指定不同的排序规则作为参数。The following query specifies the "以下查询指定值为“is
" Iceland locale and caseFirst
optional parameter with the value "upper
":upper
”的“is”冰岛locale和caseFirst
可选参数:
collection.find({type: "photograph"},
{ collation: { locale: "is", caseFirst: "upper" } }
);
Assign a Collation to an Index为索引分配排序规则¶
In the following example, we create a new index on the 在下面的示例中,我们在集合的title
field of a collection with a collation set to the "en_US
" locale.title
字段上创建一个新索引,并将排序规则设置为“en_US
”区域设置。
collection.createIndex(
{ 'title' : 1 },
{ 'collation' : { 'locale' : 'en_US' } });
The following query uses the index we created:以下查询使用我们创建的索引:
collection.find({"year": 1980}, {"collation" : {"locale" : "en_US" }})
.sort({"title": -1});
The following queries do not use the index that we created. 以下查询不使用我们创建的索引。The first query does not include a collation and the second contains a different strength value than the collation on the index.第一个查询不包括排序规则,第二个查询包含与索引排序规则不同的强度值。
// 未指定排序规则
collection.find({"year": 1980})
.sort({"title": -1});
// 排序规则与索引上的排序规则不同
collection.find({"year": 1980}, {"collation" : {"locale" : "en_US", "strength": 2 }})
.sort({"title": -1});
Collation Query Examples排序规则查询示例¶
Operations that read, update, and delete documents from a collection can use collations. 从集合中读取、更新和删除文档的操作可以使用排序规则。This section includes examples of a selection of these. 本节包括这些选择的示例。See the MongoDB manual for a full list of operations that support collation.有关支持排序规则的操作的完整列表,请参阅MongoDB手册。
find() and sort() Examplefind()
和sort()
示例¶
The following example calls both 下面的示例对使用默认二进制排序规则的集合同时调用find()
and sort()
on a collection that uses the default binary collation. find()
和sort()
。We use the German collation by setting the value of the 我们通过将locale
parameter to de
.locale
参数的值设置为de
来使用德语排序规则。
collection.find({ city: "New York" }, { collation: { locale: "de" } })
.sort({ name: 1 });
findOneAndUpdate() ExamplefindOneAndUpdate()
示例¶
The following example calls the 以下示例对使用默认二进制排序规则的集合调用findOneAndUpdate()
operation on a collection that uses the default binary collation. findOneAndUpdate()
操作。The collection contains the following documents:该集合包含以下文档:
{ "_id" : 1, "first_name" : "Hans" }
{ "_id" : 2, "first_name" : "Gunter" }
{ "_id" : 3, "first_name" : "Günter" }
{ "_id" : 4, "first_name" : "Jürgen" }
Consider the following 考虑此集合上的不指定排序规则的以下findOneAndUpdate()
operation on this collection which does not specify a collation:FordOnUpDebug()
操作:
collection.findOneAndUpdate(
{ first_name : { $lt: "Gunter" } },
{ $set: { verified: true } }
);
Since "Gunter" is the first sorted result when using a binary collation, none of the documents come lexically before and match the 由于“Gunter”是使用二进制排序规则时的第一个排序结果,因此没有一个文档在词汇上位于查询文档中的$lt
comparison operator in the query document. $lt
比较运算符之前。As a result, the operation does not update any documents.因此,该操作不会更新任何文档。
Consider the same operation with a collation specified with the locale set to 考虑与设置区域设置指定的排序规则相同的操作de@collation=phonebook
. de@collation=phonebook
。This locale specifies the 此区域设置指定collation=phonebook
option which contains rules for prioritizing proper nouns, identified by capitalization of the first letter. collation=phonebook
选项,该选项包含对专有名词进行优先级排序的规则,由第一个字母的大写字母标识。The 这个de@collation=phonebook
locale and option sorts characters with umlauts before the same characters without umlauts.de@collation=phonebook
区域设置和选项将使用umlauts的字符排序在不使用umlauts的相同字符之前。
collection.findOneAndUpdate(
{ first_name: { $lt: "Gunter" } },
{ $set: { verified: true } },
{ collation: { locale: "de@collation=phonebook" } },
);
Since "Günter" lexically comes before "Gunter" using the 因为使用de@collation=phonebook
collation specified in findOneAndUpdate()
, the operation returns the following updated document:findOneAndUpdate()
中指定的de@collation=phonebook
排序规则,词法上"Günter"出现在"Gunter"前面,该操作返回以下更新的文档:
{ lastErrorObject: { updatedExisting: true, n: 1 },
value: { _id: 3, first_name: 'Günter' },
ok: 1 }
findOneAndDelete() ExamplefindOneAndDelete()
示例¶
The following example calls the 以下示例对使用默认二进制排序规则并包含以下文档的集合调用findOneAndDelete()
operation on a collection that uses the default binary collation and contains the following documents:findOneAndDelete()
操作:
{ "_id" : 1, "a" : "16" }
{ "_id" : 2, "a" : "84" }
{ "_id" : 3, "a" : "179" }
In this example, we set the 在本例中,我们将numericOrdering
collation parameter to true
to sort numeric strings based on their numerical order instead of their lexical order.numericOrdering
排序规则参数设置为true
,以根据数字顺序而不是词汇顺序对数字字符串进行排序。
collection.findOneAndDelete(
{ a: { $gt: "100" } },
{ collation: { locale: "en", numericOrdering: true } },
);
After you run the operation above, the collection contains the following documents:运行上述操作后,集合包含以下文档:
{ "_id" : 1, "a" : "16" }
{ "_id" : 2, "a" : "84" }
If you perform the same operation without collation on the original collection of three documents, it matches documents based on the lexical value of the strings ("如果在没有对三个文档的原始集合进行排序的情况下执行相同的操作,它将根据字符串的词法值(“16
", "84
", and "179
"), and deletes the first document it finds that matches the query criteria.16
”、“84
”和“179
”)匹配文档,并删除它找到的与查询条件匹配的第一个文档。
await collection.findOneAndDelete({ a: { $gt: "100" } });
Since all the documents contain lexical values in the 由于所有文档的a
field that match the criteria (greater than the lexical value of "100
"), the operation removes the first result. a
字段中都包含符合条件的词法值(大于词法值“100
”),因此该操作将删除第一个结果。After you run the operation above, the collection contains the following documents:运行上述操作后,集合包含以下文档:
{ "_id" : 2, "a" : "84" }
{ "_id" : 3, "a" : "179" }
Aggregation Example聚合示例¶
To use collation with the aggregate operation, pass the collation document in the options field, after the array of pipeline stages.要在聚合操作中使用排序规则,请在管道阶段数组之后,在选项字段中传递排序规则文档。
The following example shows an aggregation pipeline on a collection that uses the default binary collation. 以下示例显示使用默认二进制排序规则的集合上的聚合管道。The aggregation groups the 聚合对first_name
field, counts the total number of results in each group, and sorts the results by the German phonebook (de@collation=phonebook
locale) order.first_name
字段进行分组,统计每个组中的结果总数,并按德语电话簿对结果进行排序(de@collation=phonebook
区域设置)顺序。
You can specify only one collation on an aggregation.在聚合上只能指定一个排序规则。
collection.aggregate(
[
{ $group: { "_id": "$first_name", "nameCount": { "$sum": 1 } } },
{ $sort: { "_id": 1 } },
],
{ collation: { locale: "de@collation=phonebook" } },
);