title: MongoDB group subobjects with same id and get average of values
body: <p>I have documents like this:</p>
  
  <pre><code>{
          "_id" : ObjectId("588e505fcdefc41e84c184cb"),
          "Id" : 58614891,
          "modifyDate" : 1485567717000,
          "data" : [
                  {
                          "id" : 99,
                          "stats" : {
                                  "totalDepth" : 4,
                                  "totalSpeed" : 2,
                                  "totalLostSessions" : 2,
                                  "KDI" : 8,
                          }
                  },
                  {
                          "id" : 18,
                          "stats" : {
                                  "totalDepth" : 2,
                                  "totalSpeed" : 1,
                                  "totalLostSessions" : 1,
                                  "KDI" : 2,
                          }
                  }
          ],
          "timestampPull" : 1485721695291,
          "region" : "eu",
          "Status" : 200
  }
  {
          "_id" : ObjectId("588e5060cdefc41e84c184cd"),
          "Id" : 38004043,
          "modifyDate" : 1485515118000,
          "data" : [
                  {
  
                  {
                          "id" : 18,
                          "stats" : {
                                  "totalDepth" : 5,
                                  "totalSpeed" : 3,
                                  "totalLostSessions" : 2,
                                  "KDI" : 14,
                          }
                  },
                  {
                          "id" : 62,
                          "stats" : {
                                  "totalDepth" : 1,
                                  "totalSpeed" : 0,
                                  "totalLostSessions" : 1,
                                  "KDI" : 1,
                          }
                  },
                  {
                          "id" : 0,
                          "stats" : {
                                  "totalDepth" : 155,
                                  "totalSpeed" : 70,
                                  "totalLostSessions" : 85,
                                  "KDI" : 865,
                          }
                  }
          ],
          "timestampPull" : 1485721696025,
          "region" : "na",
          "Status" : 200
  }
  </code></pre>
  
  <p>And i want to calculate average values of every stats if "data" id match. </p>
  
  <pre><code>{
                          "id" : 99,
                          "stats" : {
                                  "totalDepth" : 4,
                                  "totalSpeed" : 2,
                                  "totalLostSessions" : 2,
                                  "KDI" : 8,
                          }
  },
  {
                      "id" : 18,
                      "stats" : {
                              "totalDepth" : 3.5,
                              "totalSpeed" : 2,
                              "totalLostSessions" : 1.5,
                              "KDI" : 8,
                      }
  } ...
  </code></pre>
  
  <p>It is possible to perform such operation on mongoDB? I can easily pull every data to application and average it there, but that's not very effective. </p>