$reverseArray (집계)
정의
$reverseArray
버전 3.4에 새로 추가되었습니다.
Accepts an array expression as an argument and returns an array with the elements in reverse order.
$reverseArray
에는 다음과 같은 연산자 표현식 구문이 있습니다.{ $reverseArray: <array expression> } The argument can be any valid 표현식 as long as it resolves to an array.
행동
If the argument resolves to a value of null
or refers to a
missing field, $reverseArray
returns null
.
If the argument does not resolve to an array or null
nor refers
to a missing field, $reverseArray
returns an error.
$reverseArray
returns an empty array when the argument is an empty array.
If the argument contains subarrays, $reverseArray
only operates on the top level array elements and will not reverse the contents of subarrays.
예시 [1] | 결과 | |||
---|---|---|---|---|
|
| |||
|
| |||
|
| |||
|
| |||
|
|
[1] | The examples in the table take a literal argument. To avoid parsing
ambiguity if the literal argument is an array, you must wrap the
literal array in a $literal expression or keep the
outer array that designates the argument list (e.g. [ [ 1, 2, 3 ]
] ) to pass in the literal array [1, 2, 3] . |
예시
users
라는 이름의 컬렉션에 다음 문서가 포함되어 있습니다.
{ "_id" : 1, "name" : "dave123", "favorites" : [ "chocolate", "cake", "butter", "apples" ] } { "_id" : 2, "name" : "li", "favorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "favorites" : [ ] } { "_id" : 4, "name" : "ty" }
The following example returns an array containing the elements of
the favorites
array in reverse order:
db.users.aggregate([ { $project: { name: 1, reverseFavorites: { $reverseArray: "$favorites" } } } ])
이 연산은 다음과 같은 결과를 반환합니다.
{ "_id" : 1, "name" : "dave123", "reverseFavorites" : [ "apples", "butter", "cake", "chocolate" ] } { "_id" : 2, "name" : "li", "reverseFavorites" : [ "pie", "pudding", "apples" ] } { "_id" : 3, "name" : "ahn", "reverseFavorites" : [ ] } { "_id" : 4, "name" : "ty", "reverseFavorites" : null }