On this page本页内容
When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:在比较不同BSON类型的值时,MongoDB使用以下比较顺序,从最低到最高:
MongoDB treats some types as equivalent for comparison purposes.出于比较的目的,MongoDB将某些类型视为等价的。For instance, numeric types undergo conversion before comparison.例如,数字类型在比较之前进行转换。
By default, MongoDB uses the simple binary comparison to compare strings.默认情况下,MongoDB使用简单的二进制比较来比较字符串。
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:排序规则规范具有以下语法:
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将使用以前版本中使用的简单二进制比较来进行字符串比较。
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
或缺少字段。
MongoDB’s comparison of BSON objects uses the following order:MongoDB对BSON对象的比较使用以下顺序:
Date objects sort before Timestamp objects.日期对象在时间戳对象之前排序。
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字段进行排序时,将按排序顺序将文档视为等价的。
MongoDB sorts MongoDB按以下顺序对BinData
in the following order:BinData
进行排序: