[DOCS-11969] Examples given in '$reverseArray' documentation appear misleading Created: 15/Aug/18  Updated: 29/Oct/23  Resolved: 16/Aug/18

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: David Bradford (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 5 years, 25 weeks, 6 days ago
Epic Link: DOCSP-1769

 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 ] ] }

----------------------------

Description

Scope of changes (files that need work and how much)

Impact to other docs outside of this product

MVP (work and date?)

Resources (e.g. Scope Docs, Invision)



 Comments   
Comment by Githook User [ 16/Aug/18 ]

Author:

{'username': 'kay-kim', 'email': 'kay.kim@10gen.com', 'name': 'kay'}

Message: DOCS-11969: fix examples in reverseArray
Branch: v3.4
https://github.com/mongodb/docs/commit/780981b962e083e940954ded4b57bbc99b62f910

Comment by Githook User [ 16/Aug/18 ]

Author:

{'name': 'kay', 'email': 'kay.kim@10gen.com', 'username': 'kay-kim'}

Message: DOCS-11969: fix examples in reverseArray
Branch: v3.6
https://github.com/mongodb/docs/commit/d0a4f14e1668e38d7497aada649dcf647f1052b7

Comment by Githook User [ 16/Aug/18 ]

Author:

{'name': 'kay', 'email': 'kay.kim@10gen.com', 'username': 'kay-kim'}

Message: DOCS-11969: fix examples in reverseArray
Branch: master
https://github.com/mongodb/docs/commit/2667a2d4dbab1bc917a60ea65568c91ffd7eecd6

Generated at Thu Feb 08 08:04:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.