title: MongoDB aggregation: Aggregate array into keyed objects
body: <p>I'd like to flatten document array into a keyed objects using the aggregation functionality. Here’s an example of my documents:</p>
  
  <pre><code>[ 
      {
          "_id": 1,
          "texts": [
              { "language": "english", "text": "hello" },
              { "language": "german", "text": "hallo" },
              { "language": "french", "text": "bonjour" }
          ]
      }, …
  ]
  </code></pre>
  
  <p>Expected result:</p>
  
  <pre><code>[
       {
          "_id": 1,
          "texts": {
              "english": "hello",
              "german": "hallo",
              "french": "bonjour"
          }
      }, …
  ]
  </code></pre>
  
  <p>I’ve looked at different operators, e.g. <code>$map</code>, but this seems to be focued on transforming array to array. I’d probably need a JS equivalent of <code>$reduce</code>, but couldn’t find a way of accumulating my values into an object.</p>
  
  <p>Any ideas?</p>