Details
-
Bug
-
Resolution: Fixed
-
Major - P3
-
None
-
None
-
None
-
None
Description
----------------------------
Original Description
I was looking into the '$reverseArray' aggregation expression found here: https://docs.mongodb.com/manual/reference/operator/aggregation/reverseArray/
Most of the examples given in the 'Example' section don't actually work when put in an aggregation. Because expressions treat their arguments as an array of arguments, when an array is given to '$reverseArray` it is treated like an argument list rather than a single argument. So in order to treat the argument as an array, it either needs to be wrapped in another array or the '$literal' expression needs to be included (The '$arrayToObject' expression uses this in its examples, https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/).
Here is what I get when I attempt to run some of the examples in the shell:
> db.test.aggregate([{$project: {_id: 0, a: {$reverseArray: [1, 2, 3]}}}])
|
assert: command failed: {
|
"ok" : 0,
|
"errmsg" : "Expression $reverseArray takes exactly 1 arguments. 3 were passed in.",
|
"code" : 16020,
|
"codeName" : "Location16020"
|
} : aggregate failed
|
|
|
> db.test.aggregate([{$project: {_id: 0, a: { $reverseArray: [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] }}}])
|
assert: command failed: {
|
"ok" : 0,
|
"errmsg" : "Expression $reverseArray takes exactly 1 arguments. 2 were passed in.",
|
"code" : 16020,
|
"codeName" : "Location16020"
|
} : aggregate failed
|
And here are valid version of those expressions:
> db.test.aggregate([{$project: {_id: 0, a: { $reverseArray: {$literal: [1, 2, 3]} }}}])
|
{ "a" : [ 3, 2, 1 ] }
|
|
|
> db.test.aggregate([{$project: {_id: 0, a: { $reverseArray: {$literal: [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]} }}}])
|
{ "a" : [ [ 4, 5, 6 ], [ 1, 2, 3 ] ] }
|
----------------------------