cursor.collation()

Definition

  • cursor.collation()

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

New in version 3.4.

Specifies the collation for the cursor returnedby the db.collection.find(). To use, append to thedb.collection.find().

The cursor.collation() accepts the following collationdocument:

  1. {
  2. locale: <string>,
  3. caseLevel: <boolean>,
  4. caseFirst: <string>,
  5. strength: <int>,
  6. numericOrdering: <boolean>,
  7. alternate: <string>,
  8. maxVariable: <string>,
  9. backwards: <boolean>
  10. }

When specifying collation, the locale field is mandatory; allother collation fields are optional. For descriptions of the fields,see Collation Document.

FieldTypeDescriptionlocalestringThe ICU locale. See Supported Languages and Locales for alist of supported locales.

To specify simple binary comparison, specify locale value of"simple".strengthintegerOptional. The level of comparison to perform.Corresponds to ICU Comparison Levels.Possible values are:

ValueDescription1Primary level of comparison. Collation performscomparisons of the base characters only, ignoring otherdifferences such as diacritics and case.2Secondary level of comparison. Collation performs comparisonsup to secondary differences, such as diacritics.That is, collation performs comparisons of base characters(primary differences) and diacritics (secondary differences).Differences between base characters takes precedence oversecondary differences.3Tertiary level of comparison. Collation performs comparisonsup to tertiary differences, such as case and letter variants.That is, collation performs comparisons of base characters(primary differences), diacritics (secondary differences), andcase and variants (tertiary differences). Differences betweenbase characters takes precedence over secondary differences,which takes precedence over tertiary differences.

This is the default level.4Quaternary Level. Limited for specific use case toconsider punctuation when levels 1-3 ignore punctuationor for processing Japanese text.5Identical Level. Limited for specific use case of tiebreaker.

See ICU Collation: Comparison Levelsfor details.caseLevelbooleanOptional. Flag that determines whether to include case comparison atstrength level 1 or 2.

If true, include case comparison; i.e.

  • When used with strength:1, collation compares base charactersand case.
  • When used with strength:2, collation compares base characters,diacritics (and possible other secondary differences) and case.If false, do not include case comparison at level 1 or2. The default is false.

For more information, see ICU Collation: Case Level.caseFirststringOptional. A field that determines sort order of case differences duringtertiary level comparisons.

Possible values are:

ValueDescription“upper”Uppercase sorts before lowercase.“lower”Lowercase sorts before uppercase.“off”Default value. Similar to "lower" with slightdifferences. Seehttp://userguide.icu-project.org/collation/customizationfor details of differences.

numericOrderingbooleanOptional. Flag that determines whether to compare numeric strings as numbersor as strings.

If true, compare as numbers; i.e. "10" is greater than"2".

If false, compare as strings; i.e. "10" is less than "2".

Default is false.alternatestringOptional. Field that determines whether collation should consider whitespaceand punctuation as base characters for purposes of comparison.

Possible values are:

ValueDescription"non-ignorable"Whitespace and punctuation are considered base characters."shifted"Whitespace and punctuation are not considered base charactersand are only distinguished at strength levels greater than 3.

See ICU Collation: Comparison Levelsfor more information.

Default is "non-ignorable".maxVariablestringOptional. Field that determines up to which characters are consideredignorable when alternate: "shifted". Has no effect ifalternate: "non-ignorable"

Possible values are:

ValueDescription"punct"Both whitespaces and punctuation are “ignorable”, i.e. notconsidered base characters."space"Whitespace are “ignorable”, i.e. not considered basecharacters.

backwardsbooleanOptional. Flag that determines whether strings with diacritics sort from backof the string, such as with some French dictionary ordering.

If true, compare from back to front.

If false, compare from front to back.

The default value is false.normalizationbooleanOptional. Flag that determines whether to check if text require normalizationand to perform normalization. Generally, majority of text does notrequire this normalization processing.

If true, check if fully normalized and perform normalization tocompare text.

If false, does not check.

The default value is false.

Seehttp://userguide.icu-project.org/collation/concepts#TOC-Normalization for details.

Examples

Consider a collection foo with the following documents:

  1. { "_id" : 1, "x" : "a" }
  2. { "_id" : 2, "x" : "A" }
  3. { "_id" : 3, "x" : "á" }

The following operation specifies a query filter of x: "a". Theoperation also includes a collation option with locale: "en_US"(US English locale) and strength: 1 (compare base charactersonly; i.e. ignore case and diacritics):

  1. db.foo.find( { x: "a" } ).collation( { locale: "en_US", strength: 1 } )

The operation returns the following documents:

  1. { "_id" : 1, "x" : "a" }
  2. { "_id" : 2, "x" : "A" }
  3. { "_id" : 3, "x" : "á" }

If you do not specify the collation, i.e. db.collection.find( { x:"a" } ), the query would only match the following document:

  1. db.foo.find( { x: "a" } )

You can chain other cursor methods, such as cursor.sort()and cursor.count(), to cursor.collation():

  1. db.collection.find({...}).collation({...}).sort({...});
  2. db.collection.find({...}).collation({...}).count();

Note

You cannot specify multiple collations for an operation. Forexample, you cannot specify different collations per field, or ifperforming a find with a sort, you cannot use one collation for thefind and another for the sort.