Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11969

Examples given in '$reverseArray' documentation appear misleading

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 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 ] ] }
      

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

      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)

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            david.bradford@mongodb.com David Bradford (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 25 weeks, 6 days ago