Docs HomeMongoDB Manual

Comparison/Sort Order比较/排序顺序

When comparing values of different BSON types in sort operations, MongoDB uses the following comparison order, from lowest to highest:在排序操作中比较不同BSON类型的值时,MongoDB使用以下比较顺序,从低到高:

  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. MaxKey (internal type)
Note

$range query and aggregation operators perform comparisons only on fields where the BSON type matches the query value's type. 查询和聚合运算符仅对BSON类型与查询值的类型匹配的字段执行比较。MongoDB supports limited cross-BSON comparison through Type Bracketing.MongoDB通过类型方括号支持有限的跨BSON比较。

Numeric Types数值类型

MongoDB treats some types as equivalent for comparison purposes. 出于比较的目的,MongoDB将某些类型视为等价类型。For instance, numeric types undergo conversion before comparison.例如,数字类型在进行比较之前要进行转换。

Strings

Binary Comparison二进制比较

By default, MongoDB uses the simple binary comparison to compare strings.默认情况下,MongoDB使用简单的二进制比较来比较字符串。

Collation排序规则

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. 指定排序规则时,locale字段是必需的;所有其他排序规则字段都是可选的。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.如果没有为集合或操作指定排序规则,MongoDB将使用以前版本中使用的简单二进制比较进行字符串比较。

Arrays数组

In array comparisons:数组内比较:

  • A less-than comparison, or an ascending sort, compares the smallest elements of the array according to the BSON type sort order.小于比较或升序根据BSON类型的排序顺序比较数组中最小的元素。
  • A greater-than comparison, or a descending sort, compares the largest elements of the array according to the reverse BSON type sort order.大于比较或降序根据BSON类型的反向排序顺序比较数组中最大的元素。
  • When comparing a field whose value is a one element array (example, [ 1 ]) with non-array fields (example, 2), the comparison is for 1 and 2.当将值为单元素数组的字段(例如[1])与非数组字段(例如2)进行比较时,比较结果为12
  • A comparison of an empty array (example, [ ]) considers the empty array as less than a null value or a missing field value.空数组(例如[])的比较认为空数组小于null值或缺少字段值。

Objects

MongoDB's comparison of BSON objects uses the following order:MongoDB对BSON对象的比较使用以下顺序:

  1. Recursively compare key-value pairs in the order that they appear within the BSON object.按键值对在BSON对象中出现的顺序递归比较它们。
  2. Compare the field types. 比较字段类型。MongoDB uses the following comparison order for field types, from lowest to highest:MongoDB对字段类型使用以下比较顺序,从低到高:

    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. 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). 如果字段值相等,则比较下一个键/值对(返回步骤1)。An object without further pairs is less than an object with further pairs.没有进一步配对的对象小于有进一步配对的物体。

Dates and Timestamps日期和时间戳

Date objects sort before Timestamp objects.日期对象排序在时间戳对象之前。

Non-existent Fields不存在的字段

The comparison treats a non-existent field as if it were an empty BSON Object. 该比较将不存在的字段视为空BSON对象。As such, a sort on the a field in documents { } and { a: null } would treat the documents as equivalent in sort order.因此,对文档{ }{ a: null }中的a字段进行排序将按排序顺序将这些文档视为等价文档。

BinData

MongoDB sorts BinData in the following order:MongoDB按以下顺序对BinData进行排序:

  1. First, the length or size of the data.首先,数据的长度或大小。
  2. Then, by the BSON one-byte subtype.然后,由BSON一个字节的子类型。
  3. Finally, by the data, performing a byte-by-byte comparison.最后,通过对数据进行逐字节比较。