Skip to content

Commit 75d9d0d

Browse files
committed
DATAMONGO-1518 - Documentation.
1 parent 0b555f3 commit 75d9d0d

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Usage of the `Document` API instead of `DBObject`.
88
* <<mongo.reactive>>.
99
* Support for aggregation result streaming via Java 8 `Stream`.
10+
* Integration of collations for collection and index creation and query operations.
1011

1112
[[new-features.1-10-0]]
1213
== What's new in Spring Data MongoDB 1.10

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,85 @@ TextQuery.searching(new TextCriteria().phrase("coffee cake"));
13441344

13451345
The flags for `$caseSensitive` and `$diacriticSensitive` can be set via the according methods on `TextCriteria`. Please note that these two optional flags have been introduced in MongoDB 3.2 and will not be included in the query unless explicitly set.
13461346

1347+
[[mongo.collation]]
1348+
=== Collations
1349+
1350+
MongoDB supports since 3.4 collations for collection and index creation and various query operations. Collations define string comparison rules based on the http://userguide.icu-project.org/collation/concepts[ICU collations]. A collation document consists of various properties that are encapsulated in `Collation`:
1351+
1352+
====
1353+
[source,java]
1354+
----
1355+
Collation collation = Collation.of("fr") <1>
1356+
1357+
.strength(ComparisonLevel.secondary() <2>
1358+
.includeCase())
1359+
1360+
.numericOrderingEnabled() <3>
1361+
1362+
.alternate(Alternate.shifted().punct()) <4>
1363+
1364+
.forwardDiacriticSort() <5>
1365+
1366+
.normalizationEnabled(); <6>
1367+
----
1368+
<1> `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.
1369+
<2> Collation strength defines comparison levels denoting differences between characters. You can configure various options (case-sensitivity, case-ordering) depending on the selected strength.
1370+
<3> Specify whether to compare numeric strings as numbers or as strings.
1371+
<4> Specify whether the collation should consider whitespace and punctuation as base characters for purposes of comparison.
1372+
<5> Specify whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.
1373+
<6> Specify whether to check if text requires normalization and to perform normalization.
1374+
====
1375+
1376+
Collations can be used to create collections and indexes. If you create a collection specifying 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.
1377+
1378+
[source,java]
1379+
----
1380+
Collation french = Collation.of("fr");
1381+
Collation german = Collation.of("de");
1382+
1383+
template.createCollection(Person.class, CollectionOptions.just(collation));
1384+
1385+
template.indexOps(Person.class).ensureIndex(new Index("name", Direction.ASC).collation(german));
1386+
----
1387+
1388+
NOTE: MongoDB uses simple binary comparison if no collation is specified (`Collation.simple()`).
1389+
1390+
Using collations with collection operations is a matter of specifying a `Collation` instance in your query or operation options.
1391+
1392+
.Using collation with `find`
1393+
====
1394+
[source,java]
1395+
----
1396+
Collation collation = Collation.of("de");
1397+
1398+
Query query = new Query(Criteria.where("firstName").is("Amél")).collation(collation);
1399+
1400+
List<Person> results = template.find(query, Person.class);
1401+
----
1402+
====
1403+
1404+
.Using collation with `aggregate`
1405+
====
1406+
[source,java]
1407+
----
1408+
Collation collation = Collation.of("de");
1409+
1410+
AggregationOptions options = new AggregationOptions.Builder().collation(collation).build();
1411+
1412+
Aggregation aggregation = newAggregation(
1413+
project("tags"),
1414+
unwind("tags"),
1415+
group("tags")
1416+
.count().as("count")
1417+
).withOptions(options);
1418+
1419+
AggregationResults<TagCount> results = template.aggregate(aggregation, "tags", TagCount.class);
1420+
----
1421+
====
1422+
1423+
WARNING: Indexes are only used if the collation used for the operation and the index collation matches.
1424+
1425+
13471426
include::../{spring-data-commons-docs}/query-by-example.adoc[leveloffset=+1]
13481427
include::query-by-example.adoc[leveloffset=+1]
13491428

@@ -2241,6 +2320,8 @@ You can create standard, geospatial and text indexes using the classes `IndexDef
22412320
mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));
22422321
----
22432322

2323+
NOTE: `Index` and `GeospatialIndex` support configuration of <<mongo.collation,collations>>.
2324+
22442325
[[mongo-template.index-and-collections.access]]
22452326
=== Accessing index information
22462327

@@ -2281,6 +2362,8 @@ mongoTemplate.dropCollection("MyNewCollection");
22812362
* *dropCollection* Drop the collection
22822363
* *getCollection* Get a collection by name, creating it if it doesn't exist.
22832364

2365+
NOTE: Collection creation allows customization via `CollectionOptions` and supports <<mongo.collation,collations>>.
2366+
22842367
[[mongo-template.commands]]
22852368
== Executing Commands
22862369

0 commit comments

Comments
 (0)