title: Apply $group aggregation with changeable array size
body: <p>I have the following documents:</p>
  <pre><code>//doc 1
  {
      &quot;_id&quot; : ObjectId(&quot;5ac392f15c592414ec99d630&quot;), 
      &quot;nv&quot; : &quot;de&quot;, 
      &quot;nid&quot; : 1.0, 
      &quot;coc&quot; : 3.0, 
      &quot;seg&quot; : {
          &quot;segid&quot; : 0.0, 
          &quot;attv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 5.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 4.0
              }
          ], 
          &quot;devattv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 3.0, 
                  &quot;t&quot; : 2.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 2.0
              }
          ]
      }
  }
  //doc 2
  { 
      &quot;_id&quot; : ObjectId(&quot;5ac396565c592414ec99d64f&quot;), 
      &quot;nv&quot; : &quot;de&quot;, 
      &quot;nid&quot; : 1.0, 
      &quot;coc&quot; : 4.0, 
      &quot;seg&quot; : {
          &quot;segid&quot; : 0.0, 
          &quot;attv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 5.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 4.0
              }
          ], 
          &quot;devattv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 3.0, 
                  &quot;t&quot; : 2.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 2.0
              }
          ]
      }
  }
  //doc 3
  { 
      &quot;_id&quot; : ObjectId(&quot;5ac498f15c592414ec99d6c8&quot;), 
      &quot;nv&quot; : &quot;de&quot;, 
      &quot;nid&quot; : 1.0, 
      &quot;coc&quot; : 6.0, 
      &quot;seg&quot; : {
          &quot;segid&quot; : 0.0, 
          &quot;attv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 5.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 4.0
              }
          ], 
          &quot;devattv&quot; : [
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 1.0, 
                  &quot;t&quot; : 2.0
              }, 
              {
                  &quot;id&quot; : 1.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 3.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 1.0, 
                  &quot;t&quot; : 1.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 1.0, 
                  &quot;t&quot; : 2.0
              }, 
              {
                  &quot;id&quot; : 2.0, 
                  &quot;v&quot; : 2.0, 
                  &quot;t&quot; : 3.0
              }
          ]
      }
  }
  </code></pre>
  <p>The end result that I need is this:</p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;nv&quot;: &quot;de&quot;,
      &quot;coc&quot;: 13,
      &quot;attv&quot;: [{
          &quot;_id&quot;: 1,
          &quot;vsum&quot;: 15
      }, {
          &quot;_id&quot;: 2,
          &quot;vsum&quot;: 12
      }],
      &quot;devattv&quot;: [{
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 6,
          &quot;t&quot;: 1
      }, {
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 7,
          &quot;t&quot;: 2
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 5,
          &quot;t&quot;: 1
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 5,
          &quot;t&quot;: 2
      }, {
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 2,
          &quot;t&quot;: 3
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 2,
          &quot;t&quot;: 3
      }]
  }
  </code></pre>
  <p>The aggregation pipeline that I've tried is this:</p>
  <pre><code>db.collection.aggregate([
  {
      &quot;$unwind&quot;: {
          &quot;path&quot;: &quot;$seg.attv&quot;,
          &quot;preserveNullAndEmptyArrays&quot;: true
      }
  }, {
      &quot;$unwind&quot;: {
          &quot;path&quot;: &quot;$seg.devattv&quot;,
          &quot;preserveNullAndEmptyArrays&quot;: true
      }
  },
  {
    &quot;$redact&quot;: {
      &quot;$cond&quot;: [
      { &quot;$eq&quot;: [ &quot;$seg.attv.id&quot;, &quot;$seg.devattv.id&quot; ] },
      &quot;$$KEEP&quot;,
      &quot;$$PRUNE&quot;
      ]
      }
  },
  {
      &quot;$group&quot;: {
          &quot;_id&quot;: {
              &quot;nid&quot;: &quot;$nid&quot;,
              &quot;attvid&quot;: &quot;$seg.attv.id&quot;,
              &quot;devattvid&quot;: &quot;$seg.devattv.id&quot;,
              &quot;t&quot;: &quot;$seg.devattv.t&quot;
          },
          &quot;vsum&quot;: { &quot;$sum&quot;: &quot;$seg.attv.v&quot;},
          &quot;dvsum&quot;: { &quot;$sum&quot;: &quot;$seg.devattv.v&quot;},
          &quot;nv&quot;: { &quot;$first&quot;: &quot;$nv&quot; },
          &quot;coc&quot;: { &quot;$sum&quot;: &quot;$coc&quot; }
      }
  },
  {
      &quot;$group&quot;: {
          &quot;_id&quot;: &quot;$_id.nid&quot;,
          &quot;nv&quot;: { &quot;$first&quot;: &quot;$nv&quot; },
          &quot;coc&quot;: { &quot;$first&quot;: &quot;$coc&quot; },
          &quot;attv&quot;: {
              &quot;$addToSet&quot;: {
                  &quot;_id&quot;: {
                      &quot;$ifNull&quot;: [&quot;$_id.attvid&quot;, 0]
                  },
                  &quot;vsum&quot;: &quot;$vsum&quot;,
              }
          },
          &quot;devattv&quot;: {
              &quot;$addToSet&quot;: {
                  &quot;_id&quot;: {
                      &quot;$ifNull&quot;: [&quot;$_id.devattvid&quot;, 0]
                  },
                  &quot;dvsum&quot;: &quot;$dvsum&quot;,
                  &quot;t&quot;: &quot;$_id.t&quot;
              }
          }
      }
  }
  ])
  </code></pre>
  <p>But the end result that I get is wrong:</p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;nv&quot;: &quot;de&quot;,
      &quot;coc&quot;: 6,
      &quot;attv&quot;: [{
          &quot;_id&quot;: 1,
          &quot;vsum&quot;: 15
      }, {
          &quot;_id&quot;: 2,
          &quot;vsum&quot;: 12
      }, {
          &quot;_id&quot;: 1,
          &quot;vsum&quot;: 5
      }, {
          &quot;_id&quot;: 2,
          &quot;vsum&quot;: 4
      }],
      &quot;devattv&quot;: [{
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 6,
          &quot;t&quot;: 1
      }, {
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 7,
          &quot;t&quot;: 2
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 5,
          &quot;t&quot;: 1
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 5,
          &quot;t&quot;: 2
      }, {
          &quot;_id&quot;: 1,
          &quot;dvsum&quot;: 2,
          &quot;t&quot;: 3
      }, {
          &quot;_id&quot;: 2,
          &quot;dvsum&quot;: 2,
          &quot;t&quot;: 3
      }]
  }
  </code></pre>
  <p><em>doc 1</em> and <em>doc 2</em> have in <code>attv</code> and <code>devattv</code> array size 2 but <em>doc 3</em> has array size 2 for <code>attv</code> and array size 3 for <code>devattv</code>.
  I understand why the aggregation pipeline produces this result, but I don't know how I can solve the problem. Anyone has any suggestions?</p>
  <h1>Edit</h1>
  <p>In case it wasn't clear in the aggregation snippet what I'm trying to achieve. I'm trying to sum the <code>v</code>'s from the flattened array <code>attv</code> as well as the <code>v</code>'s from the other flattened array <code>devattv</code>. As seen here:</p>
  <pre><code>&quot;vsum&quot;: { &quot;$sum&quot;: &quot;$seg.attv.v&quot;},
  &quot;dvsum&quot;: { &quot;$sum&quot;: &quot;$seg.devattv.v&quot;}
  </code></pre>
  <p>and grouped by these fields:</p>
  <pre><code>&quot;nid&quot;: &quot;$nid&quot;,
  &quot;attvid&quot;: &quot;$seg.attv.id&quot;,
  &quot;devattvid&quot;: &quot;$seg.devattv.id&quot;,
  &quot;t&quot;: &quot;$seg.devattv.t&quot;
  </code></pre>
  <p>The problem is caused by the 3rd document. If I do this with <code>doc 1</code> and <code>doc 2</code> only then everything is fine. But once I add <code>doc 3</code> where the array size doesn't match the others, the results are wrong.</p>