Definition定义
$sortArrayNew in version 5.2.在版本5.2中新增。Sorts an array based on its elements. The sort order is user specified.根据数组元素对数组进行排序。排序顺序由用户指定。$sortArrayhas the following syntax:具有以下语法:$sortArray: {
input: <array>,
sortBy: <sort spec>
}Field字段Type类型Description描述inputexpression表达式The array to be sorted.要排序的数组。The result is如果表达式为以下情况,结果将是nullif the expression:null:Evaluates to missing.评估为缺失。Evaluates to计算结果为null.null。Evaluates to计算结果为undefined.undefined。
If the expression evaluates to any other non-array value, the operation results in an error.如果表达式的计算结果为任何其他非数组值,则该操作将导致错误。sortBydocument or int (文档或1or-1)int(1或-1)A value that specifies a sort ordering. Its possible values are指定排序顺序的值。它的可能值是1,-1, or a document.1、-1或文档。If the value of如果sortByis a document, sort ordering refers to field-direction pairs that determine how to sort the array elements, such as{ name: 1, age: -1 }where1indicates ascending order and-1indicates descending order.sortBy的值是一个文档,则排序顺序是指确定如何对数组元素进行排序的字段方向对,例如{ name: 1, age: -1 },其中1表示升序,-1表示降序。If the value of如果sortByis1, MongoDB sorts the array in ascending order. If the value ofsortByis-1, MongoDB sorts the array in descending order.sortBy的值为1,MongoDB将按升序对数组进行排序。如果sortBy的值为-1,MongoDB将按降序对数组进行排序。
Behavior行为
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操作中的行为相同。
Sort by Document Fields按文档字段排序
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> }
}Sort by Value按值排序
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>
}Considerations注意事项
There is no implicit array traversal on the sort key.排序键上没有隐式数组遍历。Positional operators are not supported. A field name like "values.1" denotes a sub-field called "1" in the不支持位置运算符。像“values.1”这样的字段名表示valuesarray. It does not refer to the item at index 1 in thevaluesarray.values数组中名为“1”的子字段。它不引用values数组中索引1处的项。When a whole array is sorted, the sort is lexicographic. The aggregation当对整个数组进行排序时,排序是字典式的。聚合$sortstage, behaves differently.$sort阶段的行为不同。See有关更多详细信息,请参阅$sortfor more details.$sort。When an array is sorted by a field, any documents or scalars that do not have the specified field are sorted equally. The resulting sort order is undefined.当数组按字段排序时,任何没有指定字段的文档或标量都会被同等排序。结果排序顺序未定义。nullvalues and missing values sort equally.值和缺失值排序相等。
Sort Stability排序稳定性
The stability of the sort is not specified. Users should not rely on 未指定排序的稳定性。用户不应依赖$sortArray to use a particular sorting algorithm.$sortArray来使用特定的排序算法。
Examples示例
The examples in this section use a 本节中的示例使用team array in an engineers collection.engineers集合中的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. Each element of team has nested sub-elements: name, age, and address. The following examples show how to sort the team array using these sub-elements.team数组有三个元素。team的每个元素都有嵌套的子元素:name、age和address。以下示例显示了如何使用这些子元素对team数组进行排序。
Sort on a Field按字段排序
The following operation sorts on the 以下操作对name field within the team array:team数组中的name字段进行排序:
db.engineers.aggregate( [
{ $project:
{
_id: 0,
result:
{
$sortArray: { input: "$team", sortBy: { name: 1 } }
}
}
}
] )
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' }
}
]
}Sort on a Subfield按子字段排序
The following operation uses dot notation to sort the array based on the 以下操作使用点符号根据address subfield:address子字段对数组进行排序:
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' }
}
]
}Sort on Multiple Fields在多个字段上排序
The following operation specifies multiple index fields to do a compound sort within the 以下操作指定了多个索引字段,以便在$sortArray operation:$sortArray操作中进行复合排序:
db.engineers.aggregate( [
{
$project:
{
_id: 0,
result:
{
$sortArray:
{
input: "$team",
sortBy: { age: -1, name: 1 }
}
}
}
}
] )
Example output:示例输出:
{
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' }
}
]
}Sort an Array of Integers对整数数组进行排序
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. 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.输入数组在位置0和位置2有一个“1”。这些元素在结果中被组合在一起,但它们并不能保证这组元素相对于其原始顺序是如何排序的。
Sort on Mixed Type Fields按混合类型字段排序
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 } }
]
}