[SERVER-23310] Add an expression to convert an array of pairs to an object Created: 23/Mar/16  Updated: 19/Jul/17  Resolved: 07/Mar/17

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.4.4, 3.5.5

Type: New Feature Priority: Major - P3
Reporter: Charlie Swanson Assignee: Tess Avitabile (Inactive)
Resolution: Done Votes: 6
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
is depended on by CSHARP-1940 Add an expression to convert an array... Backlog
Documented
Duplicate
is duplicated by SERVER-5947 Add ability to project key names as v... Closed
is duplicated by SERVER-23729 Crafting subdocuments with the aggreg... Closed
Related
related to DOCS-10459 The documentation for $objectToArray ... Closed
related to DRIVERS-358 Aggregation Framework Support for 3.6 Closed
related to SERVER-18794 Add an aggregation expression to conv... Closed
Backwards Compatibility: Fully Compatible
Backport Requested:
v3.4
Sprint: Query 2017-03-27
Participants:

 Description   

In many cases, it would be useful to construct an object from an array of key/value pairs. The new expression should be designed to work with the expression requested in SERVER-18794. For example, we could add an $arrayToObject expression, which could do the following:

db.foo.drop();
db.foo.insert({
  myArray: [ ["price", 200], ["item", "mango"] ],
  document: {x: 1, y: 2, z: 2},
  docs: [
    {email: "charlie", count: 4, blah: "foo"},
    {email: "eliot", count: 500, blah: "bar"},
  ]
});
db.foo.aggregate([{
    $project: {
        convertedFrom2dArray: {$arrayToObject: "$myArray"},
        noOp: {$arrayToObject: {$objectToArray: "$document"}},
              aggregated: {$arrayToObject: {$zip: ["$docs.email", 
                                                   "$docs.count"]}}}
    }
}]);
 
// Output:
{
  _id: xxx,
  convertedFrom2dArray: {"price": 200, "item": "mango"},
  noOp: {x: 1, y: 2, z: 2},
  aggregated: {charlie: 4, eliot: 500},
}

The expression should accept an array of pairs either represented as

  1. an array of two-element arrays (like [ ["price", 200], ["item", "mango"] ] above), or
  2. an array of two-field objects with key names k and v.

In the latter case, the expression would permit conversion of [{k: "price", v: 200}, {k: "item", v: "mango"}] to {price: 200, item: "mango"}.



 Comments   
Comment by Githook User [ 28/Mar/17 ]

Author:

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

Message: SERVER-23310 Add aggregation expression $arrayToObject

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

Comment by Githook User [ 07/Mar/17 ]

Author:

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

Message: SERVER-23310 Add aggregation expression $arrayToObject

Signed-off-by: Tess Avitabile <tess.avitabile@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/5b8b1ca6364342d5a1bf21ec6c707edfae0f3555

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