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

Add an aggregation expression to convert an object to an array of key, value pairs

    Details

    • Backwards Compatibility:
      Fully Compatible
    • Backport Requested:
      v3.4
    • Sprint:
      Query 2017-03-06

      Description

      In many cases, it would be useful to convert an object to an array. We should add an aggregation expression to do so. For example, it could be called $objectToArray, and do something like this:

      > db.foo.insert({_id: 0, subDoc: {a: 1, b: 3, c: "example"}});
      > db.foo.aggregate([{$project: {expanded: {$objectToArray: "$subDoc"}}}])
      {_id: 0, expanded: [{k: "a", v: 1}, {k: "b", v: 3}, {k: "c", v: "example"}]}
      

      Original Description

      Currently, if a collection contains the following document:

      {_id: 0, data: {a: 1, b: 2, c: 3}}
      

      There is no way to unwind that one document into the following three documents:

      {_id: 0, data: {a: 1}}
      {_id: 0, data: {b: 2}}
      {_id: 0, data: {c: 3}}
      

      One way to accomplish this would be to add a stage to unwind documents, e.g.

      db.coll.aggregate([{$objectToArray: {field: '$data'}}])
      

      Which would yield the following document, which could then be unwound to produce the desired result:

      {_id: 0, data: [{a: 1}, {b: 2}, {c: 3}]}
      

        Issue Links

          Activity

          Hide
          david.storch David Storch added a comment -

          Tess Avitabile, what was the correct pull request number? We should ensure that it is linked to this ticket for future reference.

          Show
          david.storch David Storch added a comment - Tess Avitabile , what was the correct pull request number? We should ensure that it is linked to this ticket for future reference.
          Hide
          tess.avitabile Tess Avitabile added a comment -

          Wan Bachtiar, was there a pull request on the public repo, or just on your fork? I reviewed the one that was on your fork.

          Show
          tess.avitabile Tess Avitabile added a comment - Wan Bachtiar , was there a pull request on the public repo, or just on your fork? I reviewed the one that was on your fork.
          Hide
          wan.bachtiar Wan Bachtiar added a comment -

          Tess Avitabile correct. The pull-request was just on my fork https://github.com/sindbach/mongo, and not a pull-request on the https://github.com/mongodb/mongo
          Please let me know if there's anything I could do to help rectify

          Show
          wan.bachtiar Wan Bachtiar added a comment - Tess Avitabile correct. The pull-request was just on my fork https://github.com/sindbach/mongo , and not a pull-request on the https://github.com/mongodb/mongo Please let me know if there's anything I could do to help rectify
          Hide
          tess.avitabile Tess Avitabile added a comment -

          Okay, thank you. It is fine to leave the commit message as is. Sorry for the mistake!

          Show
          tess.avitabile Tess Avitabile added a comment - Okay, thank you. It is fine to leave the commit message as is. Sorry for the mistake!
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'sindbach', u'name': u'Wan Bachtiar', u'email': u'sindbach@gmail.com'}

          Message: SERVER-18794 Add aggregation pipeline expression $objectToArray

          Closes #1

          Signed-off-by: Tess Avitabile <tess.avitabile@mongodb.com>
          (cherry picked from commit 6a6bffee00e95776f7dd50e96aa0b8874ca7a01d)
          Branch: v3.4
          https://github.com/mongodb/mongo/commit/02eeab70014675b3c41839197c5f684ae6534f95

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'sindbach', u'name': u'Wan Bachtiar', u'email': u'sindbach@gmail.com'} Message: SERVER-18794 Add aggregation pipeline expression $objectToArray Closes #1 Signed-off-by: Tess Avitabile <tess.avitabile@mongodb.com> (cherry picked from commit 6a6bffee00e95776f7dd50e96aa0b8874ca7a01d) Branch: v3.4 https://github.com/mongodb/mongo/commit/02eeab70014675b3c41839197c5f684ae6534f95

            People

            • Votes:
              15 Vote for this issue
              Watchers:
              30 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                  Agile