Working with Collations
1. Understanding Collation Options
| Field | Values | Effect |
|---|---|---|
| locale | e.g. "en", "fr", "de_AT" | Language rules |
| strength | 1-5 | Comparison level (1=primary, 3=default) |
| caseLevel | bool | Case sensitivity at strength 1 or 2 |
| caseFirst | "upper","lower","off" | Tiebreak order |
| numericOrdering | bool | "2" < "10" when true |
| alternate | "non-ignorable","shifted" | Punctuation handling |
| maxVariable | "punct","space" | Variable elements |
| backwards | bool | Reverse secondary (French) |
| normalization | bool | NFD normalize before compare |
2. Setting Collection-Level Collation
| Form | Effect |
|---|---|
createCollection("c", {collation: {...}}) | Default for all ops |
| Immutable | Cannot change after creation |
3. Using Query-Level Collation
| Form | Effect |
|---|---|
find(...).collation({...}) | Override per query |
| Index use | Requires matching index collation |
db.users.find({ name: "alice" }).collation({ locale: "en", strength: 2 });
4. Setting Index Collation
| Form | Notes |
|---|---|
createIndex({name:1}, {collation:{...}}) | Indexed values sorted per collation |
| Match required | Query collation must equal index collation |
5. Configuring Case Sensitivity
| strength | Effect |
|---|---|
| 1 | Base letters only |
| 2 | + accents |
| 3 (default) | + case |
| caseLevel: true | Force case-significance at lower strengths |
6. Using Diacritic Sensitivity
| strength | café vs cafe |
|---|---|
| 1 | Equal |
| ≥ 2 | Different |
7. Using Numeric Ordering
| Setting | Effect |
|---|---|
| numericOrdering: false (default) | "10" < "2" lexicographically |
| numericOrdering: true | "2" < "10" numerically |
8. Setting Alternate Handling
| alternate | Effect |
|---|---|
| "non-ignorable" (default) | Punctuation participates |
| "shifted" | Spaces/punctuation ignored except at strength 4 |
9. Configuring Normalization
| Setting | Use |
|---|---|
| normalization: true | Compare canonical forms (NFD) |
| Cost | Slightly slower |
10. Using Locale-Specific Sorting
| Locale | Sort Rules |
|---|---|
| de | ä = a + e for some sorts |
| sv | å, ä, ö at end |
| tr | i / I distinct from ı / İ |
| zh | Pinyin order |