Docs Menu
Docs Home
/ /

Comparison/Sort Order

When comparing values of different BSON types in sort operations, MongoDB uses the following comparison order, from lowest to highest:

  1. MinKey (internal type)

  2. Null

  3. Numbers (ints, longs, doubles, decimals)

  4. Symbol, String

  5. Object

  6. Array

  7. BinData

  8. ObjectId

  9. Boolean

  10. Date

  11. Timestamp

  12. Regular Expression

  13. JavaScript Code

  14. JavaScript Code with Scope

  15. MaxKey (internal type)

Aggregation expressions make comparisons across types. For more information, see Comparison Operators.

Note

MongoDB enforces comparisons with Comparison Query Predicate Operators only on documents where the BSON type of the target field matches the query operand type through Type Bracketing.

MongoDB treats some types as equivalent for comparison purposes. For instance, all numeric types are considered equivalent in comparisons.

By default, MongoDB uses the simple binary comparison to compare strings.

Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

Collation specification has the following syntax:

{
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

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

If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

When you sort on a field that contains an array:

  • An ascending sort compares the smallest elements of the array according to the BSON type sort order.

  • A descending sort compares the largest elements of the array according to the reverse BSON type sort order.

  • A sort on a field whose value is a one element array (for example, [ 1 ]) and a field whose value is not an array (for example, 2) sorts on 1 and 2.

  • A sort places an empty array (for example, [ ]) before a null value or a missing field value.

  • A sort of a nested array (for example, [[1, 2], [3, 4]]) sorts any array after the outermost array lexicographically.

When you query a field that contains an array:

  • Comparison query predicate operators, such as $lt and $gt, enforce type bracketing when the query value is an array.

  • If the target field's value is an array, the operator performs a type-bracketed comparison element-wise over the array.

  • Comparison operators compare arrays lexicographically.

MongoDB's comparison of BSON objects uses the following order:

  1. Recursively compare key-value pairs in the order that they appear within the BSON object.

  2. Compare the field types. MongoDB uses the following comparison order for field types, from lowest to highest:

    1. MinKey (internal type)

    2. Null

    3. Numbers (ints, longs, doubles, decimals)

    4. Symbol, String

    5. Object

    6. Array

    7. BinData

    8. ObjectId

    9. Boolean

    10. Date

    11. Timestamp

    12. Regular Expression

    13. JavaScript Code

    14. JavaScript Code with Scope

    15. MaxKey (internal type)

  3. If the field types are equal, compare the key field names.

  4. If the key field names are equal, compare the field values.

  5. If the field values are equal, compare the next key/value pair (return to step 1). An object without further pairs is less than an object with further pairs.

Date objects sort before Timestamp objects.

The comparison treats a non-existent field as if it were null. A sort on the a field in documents { } and { a: null } would treat the documents as equivalent in sort order.

MongoDB sorts BinData in the following order:

  1. First, the length or size of the data.

  2. Then, by the BSON one-byte subtype.

  3. Finally, by the data, performing a byte-by-byte comparison on unsigned bytes.

Back

BSON Types

On this page