Add an expression to convert an array of pairs to an object

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Won't Do
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: API
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      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"}.

            Assignee:
            Unassigned
            Reporter:
            Rathi Gnanasekaran (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: