On this page本页内容
$sortArray
New in version 5.2.在版本5.2中新增。
Sorts an array based on its elements. 根据数组的元素对数组进行排序。The sort order is user specified.排序顺序由用户指定。
$sortArray
has the following syntax:语法如下:
$sortArray: { input: <array>, sortBy: <sort spec> }
input |
| |
sortBy | document |
The $sortArray
expression orders the input
array according to the sortBy
specification.$sortArray
表达式根据sortBy
规范对input
数组排序。
The $sortArray
syntax and semantics are the same as the behavior in a $push
operation modified by $sort
.$sortArray
语法和语义与$sort
修改的$push
操作中的行为相同。
If the array elements are documents, you can sort by a document field. 如果数组元素是文档,则可以按文档字段排序。Specify the field name and a sort direction, ascending (指定字段名称和排序方向,升序(1
), or descending (-1
).1
)或降序(-1
)。
{ input: <array-of-documents>, sortBy: { <document-field>: {sort-direction> } }
To sort the whole array by value, or to sort by array elements that are not documents, identify the input array and specify 1 for an ascending sort or -1 for descending sort in the 要按值对整个数组进行排序,或按非文档的数组元素进行排序,请标识输入数组,并在sortBy
parameter.sortBy
参数中指定1表示升序排序,指定-1表示降序排序。
{ input: <array-of-documents>, sortBy: { sort-direction> } }
values
array. values
数组中名为“1”的子字段。values
array.values
数组中索引1处的项。$sort
stage, behaves differently. $sort
阶段的行为有所不同。$sort
for more details.$sort
。null
The stability of the sort is not specified. Users should not rely on 未指定排序的稳定性。用户不应该依赖$sortArray
to use a particular sorting algorithm.$sortArray
来使用特定的排序算法。
Create the 创建team
collection:team
集合:
db.engineers.insertOne( { "team": [ { "name": "pat", "age": 30, "address": { "street": "12 Baker St", "city": "London" } }, { "name": "dallas", "age": 36, "address": { "street": "12 Cowper St", "city": "Palo Alto" } }, { "name": "charlie", "age": 42, "address": { "street": "12 French St", "city": "New Brunswick" } } ] } )
The team
array has three elements. team
数组有三个元素。Each element of team
has nested sub-elements: name
, age
, and address
. team
的每个元素都有嵌套的子元素:name
、age
和address
。The following examples show how to sort the 以下示例显示了如何使用这些子元素对team
array using these sub-elements.team
数组进行排序。
Sort on a field within an array element:按数组元素中的字段排序:
db.engineers.aggregate( [ { $project: { _id: 0, result: { $sortArray: { input: "$team", sortBy: { name: 1 } } } } } ] )
The name
field is a sub-element in the team
array. name
字段是team
数组中的子元素。The operation returns the following results:该操作返回以下结果:
{ result: [ { name: 'charlie', age: 42, address: { street: '12 French St', city: 'New Brunswick' } }, { name: 'dallas', age: 36, address: { street: '12 Cowper St', city: 'Palo Alto' } }, { name: 'pat', age: 30, address: { street: '12 Baker St', city: 'London' } } ] }
The address
field is a document with subfields of its own. address
字段是一个具有自己的子字段的文档。Use dot notation to sort the array based on a subfield:使用点表示法根据子字段对数组进行排序:
db.engineers.aggregate( [ { $project: { _id: 0, result: { $sortArray: { input: "$team", sortBy: { "address.city": -1 } } } } } ] )
The sort direction is descending because the 排序方向为降序,因为sortBy
value is "-1".sortBy
值为“-1”。
{ result: [ { name: 'dallas', age: 36, address: { street: '12 Cowper St', city: 'Palo Alto' } }, { name: 'charlie', age: 42, address: { street: '12 French St', city: 'New Brunswick' } }, { name: 'pat', age: 30, address: { street: '12 Baker St', city: 'London' } } ] }
Specify multiple index fields to do a compound sort:指定多个索引字段以进行复合排序:
db.engineers.aggregate( [ { $project: { _id: 0, result: { $sortArray: { input: "$team", sortBy: { age: -1, name: 1 } } } } } ] )
Example output:示例输出:
{ name: 'charlie', age: 42, address: { street: '12 French St', city: 'New Brunswick' } }, { name: 'dallas', age: 36, address: { street: '12 Cowper St', city: 'Palo Alto' } }, { name: 'pat', age: 30, address: { street: '12 Baker St', city: 'London' } }
This example specifies an input array directly. 此示例直接指定输入数组。The values are all the same type, Int32:这些值都是相同的类型,Int32:
db.engineers.aggregate( [ { $project: { _id: 0, result: { $sortArray: { input: [ 1, 4, 1, 6, 12, 5 ], sortBy: 1 } } } } ] )
Example output:示例输出:
[ { result: [ 1, 1, 4, 5, 6, 12 ] } ]
The input array has a "1" in position 0 and position 2. 输入数组在位置0和位置2有一个“1”。The ones are grouped together in the results, but their are no guarantees regarding how the group of ones is sorted relative to their original order.它们在结果中被分组在一起,但它们不能保证一组元素是如何相对于它们的原始顺序进行排序的。
This example specifies an input array directly. 此示例直接指定输入数组。The values have different types:这些值有不同的类型:
db.engineers.aggregate( [ { $project: { _id: 0, result: { $sortArray: { input: [ 20, 4, { a: "Free" }, 6, 21, 5, "Gratis", { a: null }, { a: { sale: true, price: 19 } }, Decimal128( "10.23" ), { a: "On sale" } ], sortBy: 1 } } } } ] )
Example output:示例输出:
{ result: [ 4, 5, 6, Decimal128("10.23"), 20, 21, 'Gratis', { a: null }, { a: 'Free' }, { a: 'On sale' }, { a: { sale: true, price: 19 } } ] }
The results are ordered.结果是有序的。
In contrast, after changing the 相反,在将sortBy
field to sort on the one of the document fields, sortBy: { a: 1 }
, the sort order for the scalar and null
values is undefined:sortBy
字段更改为在其中一个文档字段sortBy: { a: 1}
上排序后,标量值和null
值的排序顺序未定义:
{ result: [ 20, 4, 6, 21, 5, 'Gratis', { a: null }, Decimal128("10.23"), { a: 'Free' }, { a: 'On sale' }, { a: { sale: true, price: 19 } } ] }