title: Grouping in MongoDB and adding to Group
body: <p>I have a <code>MeterReadings</code> collection that looks like the following.</p>
  <pre><code>{
          &quot;_id&quot; : ObjectId(&quot;5fc768b33561870a262813c6&quot;),
          &quot;installedAppId&quot; : &quot;A&quot;,
          &quot;readings&quot; : [
                  {
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984662,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 100
                  }
          ]
  }
  {
          &quot;_id&quot; : ObjectId(&quot;5fc768c73561870a262813c7&quot;),
          &quot;installedAppId&quot; : &quot;B&quot;,
          &quot;readings&quot; : [
                  {
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984662,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 200
                  }
          ]
  }
  {
          &quot;_id&quot; : ObjectId(&quot;5fc768e43561870a262813c8&quot;),
          &quot;installedAppId&quot; : &quot;A&quot;,
          &quot;readings&quot; : [
                  {
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984672,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 300
                  }
          ]
  }
  </code></pre>
  <p>My desired output is to group by <code>installedAppId</code> and then have each <code>readings</code> in one entry attached to it from all the matching <code>installedAppId</code>'s. Shown below is what I am aiming for.</p>
  <pre><code>{
          &quot;_id&quot; : ObjectId(&quot;5fc768b33561870a262813c6&quot;),
          &quot;installedAppId&quot; : &quot;A&quot;,
           &quot;readings&quot; : [
                  {
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984662,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 100
                  },{
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984672,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 300
                  }
          ]
  }
  {
          &quot;_id&quot; : ObjectId(&quot;5fc768c73561870a262813c7&quot;),
          &quot;installedAppId&quot; : &quot;B&quot;,
          &quot;readings&quot; : [
                  {
                          &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                          &quot;t&quot; : 1606902984662,
                          &quot;u&quot; : &quot;W&quot;,
                          &quot;v&quot; : 200
                  }
          ]
  
  }
  </code></pre>
  <p>Grouping by <code>installedAppId</code> does return two groups using the data above.</p>
  <pre><code>&gt; db.MeterReadings.aggregate([ {$group: {_id: {installedAppId: &quot;$installedAppId&quot;}}} ])
  { &quot;_id&quot; : { &quot;installedAppId&quot; : &quot;B&quot; } }
  { &quot;_id&quot; : { &quot;installedAppId&quot; : &quot;A&quot; } }
  </code></pre>
  <p>As each reading is different though adding <code>readings</code> as another entry in <code>$group</code> is the same as just querying the entire database.</p>
  <pre><code>&gt; db.MeterReadings.aggregate([ {$group: {_id: {installedAppId: &quot;$installedAppId&quot;, readings: &quot;$readings&quot;}}} ]).pretty()
  {
          &quot;_id&quot; : {
                  &quot;installedAppId&quot; : &quot;A&quot;,
                  &quot;readings&quot; : [
                          {
                                  &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                                  &quot;t&quot; : 1606902984672,
                                  &quot;u&quot; : &quot;W&quot;,
                                  &quot;v&quot; : 10.2
                          }
                  ]
          }
  }
  {
          &quot;_id&quot; : {
                  &quot;installedAppId&quot; : &quot;B&quot;,
                  &quot;readings&quot; : [
                          {
                                  &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                                  &quot;t&quot; : 1606902984662,
                                  &quot;u&quot; : &quot;W&quot;,
                                  &quot;v&quot; : 10.2
                          }
                  ]
          }
  }
  {
          &quot;_id&quot; : {
                  &quot;installedAppId&quot; : &quot;A&quot;,
                  &quot;readings&quot; : [
                          {
                                  &quot;n&quot; : &quot;daf43d66-6c3b-4553-80af-6a0b1cf97418:power&quot;,
                                  &quot;t&quot; : 1606902984662,
                                  &quot;u&quot; : &quot;W&quot;,
                                  &quot;v&quot; : 10.2
                          }
                  ]
          }
  }
  </code></pre>
  <p>Any help is welcome!</p>