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使用简单的二进制比较来比较字符串。
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将使用以前版本中用于字符串比较的简单二进制比较。
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对象的比较使用以下顺序:
Compare the field types. MongoDB uses the following comparison order for field types, from lowest to highest:比较字段类型。MongoDB对字段类型使用以下比较顺序,从最低到最高:
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
进行排序: