When comparing values of different BSON types in sort operations, MongoDB uses the following comparison order, from lowest to highest:在排序操作中比较不同BSON类型的值时,MongoDB使用以下从低到高的比较顺序:
- MinKey
(internal type)(内部类型) - Null
- Numbers
(ints, longs, doubles, decimals)(整型、长整型、双精度、十进制) - Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
Regular Expression正则表达式- JavaScript Code
- JavaScript Code with Scope
- 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仅在目标字段的BSON类型通过类型花括号与查询操作数类型匹配的文档上强制使用比较查询谓词运算符进行比较。
Numeric Types数值类型
MongoDB treats some types as equivalent for comparison purposes. For instance, all numeric types are considered equivalent in comparisons.为了便于比较,MongoDB将某些类型视为等效类型。例如,在比较中,所有数字类型都被认为是等效的。
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. For descriptions of the fields, see Collation Document.locale字段是必填的;所有其他排序字段都是可选的。有关字段的描述,请参阅排序规则文档。
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:数组内比较:
An ascending sort compares the smallest elements of the array according to the BSON type sort order.升序排序根据BSON类型排序顺序比较数组的最小元素。A descending sort compares the largest elements of the array according to the reverse BSON type sort order.降序排序根据反向BSON类型排序顺序比较数组中最大的元素。Comparison Query Predicate Operators, such as比较查询谓词运算符(如$ltand$gt, perform comparisons on arrays lexicographically.$lt和$gt)按字典顺序对数组执行比较。When comparing a field whose value is a one element array (for example,当比较值为单元素数组(例如[ 1 ]) with non-array fields (for example,2), the comparison is for1and2.[1])的字段与非数组字段(例如2)时,比较的是1和2。A comparison of an empty array (for example,空数组的比较(例如[ ]) considers the empty array as less than anullvalue or a missing field value.[])将空数组视为小于null值或缺少字段值。A comparison of a nested array (for example,嵌套数组的比较(例如,[[1, 2], [3, 4]]) compares any array after the outmost array lexicographically.[[1, 2], [3, 4]])按字典顺序比较最外层数组之后的任何数组。
Note
Comparison Query Predicate Operators enforce type-bracketing when the query is an array. If the indexed value is an array, the operator performs a type-bracketed comparison element-wise over the indexed array.当查询是数组时,比较查询谓词运算符强制使用类型括号。如果索引值是一个数组,则运算符在索引数组上执行一个类型括号内的比较元素。
Objects对象
MongoDB's comparison of BSON objects uses the following order:MongoDB对BSON对象的比较使用以下顺序:
Recursively compare key-value pairs in the order that they appear within the BSON object.按照键值对在BSON对象中出现的顺序递归比较键值对。Compare the field types. MongoDB uses the following comparison order for field types, from lowest to highest:比较字段类型。MongoDB对字段类型使用以下比较顺序,从低到高:- MinKey (internal type)
- Null
- Numbers (ints, longs, doubles, decimals)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- JavaScript Code
- JavaScript Code with Scope
- MaxKey (internal type)
If the field types are equal, compare the key field names.如果字段类型相同,请比较键字段名称。If the key field names are equal, compare the field values.如果键段名称相等,请比较字段值。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.如果字段值相等,则比较下一个键/值对(返回步骤1)。没有更多对的对象小于有更多对的物体。
Dates and Timestamps日期和时间戳
Date objects sort before Timestamp objects.日期对象排序在时间戳对象之前。
Non-existent Fields不存在的字段
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.null。对文档{ }和{ a: null }中的a字段进行排序会将文档视为排序顺序相等。
BinData
MongoDB sorts MongoDB按以下顺序对BinData in the following order:BinData进行排序:
First, the length or size of the data.首先,数据的长度或大小。Then, by the BSON one-byte subtype.然后,按BSON单字节子类型。Finally, by the data, performing a byte-by-byte comparison on unsigned bytes.最后,根据数据,对无符号字节进行逐字节比较。