title: Concaternate nested arrays in Mongo
body: <p>I have the following schema in my collection:</p>
  
  <pre><code>{
      _id: ObjectId,
      foo: [
              {
                  bar_id: ObjectId,
                  bar_list: [
                      {...},
                      {...},
                  ]
              },
              {
                  bar_id: ObjectId,
                  bar_list: [
                      {...},
                      {...},
                  ]
              }
              ...
          ]
      ],
      relations: {
          relation_type: [
              {
                  relation_id: ObjectId,
                  ...
              }
          ]
          ...
      }
  }
  
  </code></pre>
  
  <p>I wish to create a new document that has a similar structure (<code>relations</code> is not needed in the new document) as the input documents but that is an aggregation on <code>relation_id</code> and where the <code>bar_list</code> arrays of each input document has been concatenated (including duplicates).</p>
  
  <p>To illustrate:</p>
  
  <p>Document 1</p>
  
  <pre><code>{
      _id: 1,
      foo: [
              {
                  bar_id: 77,
                  bar_list: [
                      {a, b},
                      {c, d},
                  ]
              },
              {
                  bar_id: 88,
                  bar_list: [
                      {u, v},
                      {w, x},
                      {y, z},
                  ]
              }
          ]
      ],
      relations: {
          relation_type: [
              {
                  relation_id: 666,
                  ...
              }
          ]
          ...
      }
  }
  </code></pre>
  
  <p>Document 2</p>
  
  <pre><code>{
      _id: 2,
      foo: [
              {
                  bar_id: 77,
                  bar_list: [
                      {a, b},
                      {e, f},
                      {g, h},
                  ]
              },
              {
                  bar_id: 88,
                  bar_list: [
                      {u, v},
                  ]
              }
          ]
      ],
      relations: {
          relation_type: [
              {
                  relation_id: 666,
                  ...
              }
          ]
          ...
      }
  }
  </code></pre>
  
  <p>Output Document</p>
  
  <pre><code>{
      foo: [
              {
                  bar_id: 77,
                  bar_list: [
                      {a, b},
                      {c, d},
                      {a, b},
                      {e, f},
                      {g, h},
                  ]
              },
              {
                  bar_id: 88,
                  bar_list: [
                      {u, v},
                      {w, x},
                      {y, z},
                      {u, v},
                  ]
              }
          ]
      ],
  }
  
  </code></pre>
  
  <p>The first part of filtering for a match against the <code>relations.relation_type.relation_id</code> is easy. But how to I concatenate the array based on <code>bar_id</code> and format the result as required? Here is what I have so far:</p>
  
  <pre class="lang-js prettyprint-override"><code>db.getCollection('example').aggregate([
      {'$match': {'relations.relation_type.relation_id': 666}},
      // Some sore of $group, $mergeObjects or $concatArrays next?
  ])
  </code></pre>
  
  <p>Any help appreciated.</p>