Comparison/Sort Order比较/排序顺序

On this page本页内容

When comparing values of different BSON types, 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)

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排序规则

New in version 3.4.3.4版中新增。

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数组

With arrays, a less-than comparison or an ascending sort compares the smallest element of arrays, and a greater-than comparison or a descending sort compares the largest element of the arrays.对于数组,小于比较或升序排序比较数组中最小的元素,大于比较或降序排序比较数组中最大的元素。As such, when comparing a field whose value is a single-element array (e.g. [ 1 ]) with non-array fields (e.g. 2), the comparison is between 1 and 2.因此,当比较值为单元素数组(例如[1])的字段与非数组字段(例如2)时,比较是在1和2之间进行的。A comparison of an empty array (e.g. [ ]) treats the empty array as less than null or a missing field.空数组的比较(例如[])将空数组视为小于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.最后,根据数据,执行逐字节比较。