Collations

自 3.4 版以来,MongoDB 支持针对集合和索引创建以及各种查询操作的排序规则。排序规则根据 ICU collations 定义字符串比较规则。排序规则文档由 Collation 中封装的各种属性组成,如下面的列表所示:

Since version 3.4, MongoDB supports collations for collection and index creation and various query operations. Collations define string comparison rules based on the ICU collations. A collation document consists of various properties that are encapsulated in Collation, as the following listing shows:

Collation collation = Collation.of("fr")         1

  .strength(ComparisonLevel.secondary()          2
    .includeCase())

  .numericOrderingEnabled()                      3

  .alternate(Alternate.shifted().punct())        4

  .forwardDiacriticSort()                        5

  .normalizationEnabled();                       6
1 Collation 需要一个语言环境才能创建。它可以是语言环境的字符串表示、Locale(考虑语言、国家/地区和变体)或 CollationLocale。语言环境是创建所必需的。
2 Collation requires a locale for creation. This can be either a string representation of the locale, a Locale (considering language, country, and variant) or a CollationLocale. The locale is mandatory for creation.
3 校对强度定义表示字符差异的比较级别。根据所选择的强度,你可以配置各种选项(区分大小写、大小写顺序等等)。
4 Collation strength defines comparison levels that denote differences between characters. You can configure various options (case-sensitivity, case-ordering, and others), depending on the selected strength.
5 指定以数字还是字符串的形式比较数字字符串。
6 Specify whether to compare numeric strings as numbers or as strings.
7 指定校对是否应将空格和标点符号视为比较的基本字符。
8 Specify whether the collation should consider whitespace and punctuation as base characters for purposes of comparison.
9 指定带变音符号的字符串是否按照从字符串末尾开始的顺序进行排序,例如一些法语词典中的顺序。
10 Specify whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.
11 指定是否检查文本是否需要规范化,以及是否执行规范化。
12 Specify whether to check whether text requires normalization and whether to perform normalization.

排序规则可用于创建集合和索引。如果创建指定排序规则的集合,则该集合将应用于索引创建和查询,除非你指定了不同的排序规则。排序规则对于整个操作有效,不能按字段指定。

Collations can be used to create collections and indexes. If you create a collection that specifies a collation, the collation is applied to index creation and queries unless you specify a different collation. A collation is valid for a whole operation and cannot be specified on a per-field basis.

与其他元数据一样,排序规则可以通过 @Document 注释的 collation 属性从域类型派生,并在运行查询、创建集合或索引时直接应用。

Like other metadata, collations can be be derived from the domain type via the collation attribute of the @Document annotation and will be applied directly when running queries, creating collections or indexes.

当 MongoDB 在首次交互时自动创建集合时,不会使用带注释的排序规则。这需要额外的存储交互,从而延迟了整个过程。请在这些情况下使用 MongoOperations.createCollection

Annotated collations will not be used when a collection is auto created by MongoDB on first interaction. This would require additional store interaction delaying the entire process. Please use MongoOperations.createCollection for those cases.

Collation french = Collation.of("fr");
Collation german = Collation.of("de");

template.createCollection(Person.class, CollectionOptions.just(collation));

template.indexOps(Person.class).ensureIndex(new Index("name", Direction.ASC).collation(german));

如果没有指定排序规则,MongoDB 将使用简单的二进制比较(Collation.simple())。

MongoDB uses simple binary comparison if no collation is specified (Collation.simple()).

对集合操作使用排序涉及在查询或操作选项中指定 Collation 实例,如下例所示:

Using collations with collection operations is a matter of specifying a Collation instance in your query or operation options, as the following two examples show:

Example 1. Using collation with find
Collation collation = Collation.of("de");

Query query = new Query(Criteria.where("firstName").is("Amél")).collation(collation);

List<Person> results = template.find(query, Person.class);
Example 2. Using collation with aggregate
Collation collation = Collation.of("de");

AggregationOptions options = AggregationOptions.builder().collation(collation).build();

Aggregation aggregation = newAggregation(
  project("tags"),
  unwind("tags"),
  group("tags")
    .count().as("count")
).withOptions(options);

AggregationResults<TagCount> results = template.aggregate(aggregation, "tags", TagCount.class);

仅当用于操作的排序规则与索引排序规则匹配时,才会使用索引。

Indexes are only used if the collation used for the operation matches the index collation.

MongoDB Repositories 通过 @Query 注释的 collation 属性支持 Collations

MongoDB Repositories support Collations via the collation attribute of the @Query annotation.