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此排序规则方法使用ASCII标准 character values to compare and order strings.
字符值对字符串进行比较和排序。
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é.
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”区域设置排序规则:
myColl.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”Iceland语言环境和caseFirst
可选参数:
myColl.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”区域设置。
myColl.createIndex(
{ 'title' : 1 },
{ 'collation' : { 'locale' : 'en_US' } });
The following query uses the index we created:以下查询使用我们创建的索引:
myColl.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.第一个查询不包括排序规则,第二个查询包含的强度值与索引上的排序规则不同。
//no collation specified未指定排序规则
myColl.find({"year": 1980})
.sort({"title": -1});
//collation differs from the one on the index排序规则与索引上的排序规则不同
myColl.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
来使用德语排序规则。
myColl.find({ city: "New York" }, { collation: { locale: "de" } })
.sort({ name: 1 });
findOneAndUpdate()
Example示例
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:findOneAndUpdate()
操作,该操作未指定排序规则:
myColl.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
区域设置和选项将带元音变音符的字符排列在不带元音变元音的相同字符之前。
myColl.findOneAndUpdate(
{ first_name: { $lt: "Gunter" } },
{ $set: { verified: true } },
{ collation: { locale: "de@collation=phonebook" } },
);
Since "Günter" lexically comes before "Gunter" using the 由于“Günter”在词汇上位于“Gunter”之前,因此使用在de@collation=phonebook
collation specified in findOneAndUpdate()
, the operation returns the following updated document:findOneAndUpdate()
中指定的de@collation=phonebook
排序规则,该操作返回以下更新的文档:
{ lastErrorObject: { updatedExisting: true, n: 1 },
value: { _id: 3, first_name: 'Günter' },
ok: 1 }
findOneAndDelete()
Example示例
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
,以根据数字字符串的数字顺序而不是词汇顺序对其进行排序。
myColl.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”和“179”)的词法值来匹配文档,并删除找到的第一个符合查询条件的文档。16
", "84
", and "179
"), and deletes the first document it finds that matches the query criteria.
await myColl.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.在聚合上只能指定一个排序规则。
myColl.aggregate(
[
{ $group: { "_id": "$first_name", "nameCount": { "$sum": 1 } } },
{ $sort: { "_id": 1 } },
],
{ collation: { locale: "de@collation=phonebook" } },
);