Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-27964

$zip should accept array of arrays expression

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Query

      Given the following collection:

      { _id : 0, grid : [ [ 0, 0, 0, 0 ], [ 0, 1, 0, 1 ], [ 0, 0, 0, 2 ], [ 1, 0, 2, 3 ] ] }
      { _id : 1, grid : [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 2 ], [ 0, 0, 0, 4 ], [ 0, 0, 4, 5 ] ] }
      

      one would expect to be able to transpose it as follows:

      db.coll.aggregate({$project: { grid : { $zip: { inputs: "$grid" } } } })
      

      However, this results in the following error:

      2017-02-09T13:51:59.786-0500 E QUERY    [thread1] Error: command failed: {
      	"ok" : 0,
      	"errmsg" : "inputs must be an array of expressions, found string",
      	"code" : 34461,
      	"codeName" : "Location34461"
      } : aggregate failed :
      

      So, the stage needs to be written instead as:

      {
          "$project" : {
              "grid" : {
                  "$zip" : {
                      "inputs" : [
                          { "$arrayElemAt" : [ "$grid", 0 ] },
                          { "$arrayElemAt" : [ "$grid", 1 ] },
                          { "$arrayElemAt" : [ "$grid", 2 ] },
                          { "$arrayElemAt" : [ "$grid", 3 ] }
                      ]
                  }
              },
      }
      

      This is unexpected, more verbose, and only works for arrays with fixed dimensions.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            geert.bosch@mongodb.com Geert Bosch
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: