title: Mongodb Aggregate Nested Group with Recent Updated Document
body: <p>I have assigned person, status inside my collection simply like below.</p>
  
  <pre><code>[
   {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411771", "STATUS": 1, "UPDATE_DATE": ISODate("2016-12-21T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411772", "STATUS": 4, "UPDATE_DATE": ISODate("2016-12-22T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411774", "STATUS": 4, "UPDATE_DATE": ISODate("2016-12-23T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411774", "STATUS": 3, "UPDATE_DATE": ISODate("2016-12-24T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322311", "SPEC_ID" : "58411775", "STATUS": 1, "UPDATE_DATE": ISODate("2016-12-25T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322311", "SPEC_ID" : "58411779", "STATUS": 3, "UPDATE_DATE": ISODate("2016-12-23T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322322", "SPEC_ID" : "58411777", "STATUS": 1, "UPDATE_DATE": ISODate("2016-12-20T04:10:23.000Z")},
   {"ASSIGN_ID": "583f84bce58725f76b322322", "SPEC_ID" : "58411778", "STATUS": 4, "UPDATE_DATE": ISODate("2016-12-21T04:10:23.000Z")}
  ]
  </code></pre>
  
  <p>i want to group this data by <strong>ASSIGN_ID</strong> and inside that by <strong>STATUS</strong> count of each <strong>STATUS</strong> and recent updated <strong>SPEC_ID</strong> by <strong>UPDATE_DATE</strong>. same as below. (Expected Result)</p>
  
  <pre><code>[  
     {  
        "ASSIGN_ID":"583f84bce58725f76b322398",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":3,
              "COUNT":1
           },
           {  
              "STATUS":4,
              "COUNT":2
           }
        ],
        "SPEC_ID": "58411774"
     },
     {  
        "ASSIGN_ID":"583f84bce58725f76b322311",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":3,
              "COUNT":1
           }
        ],
        "SPEC_ID": "58411775"
     },
     {  
        "ASSIGN_ID":"583f84bce58725f76b322322",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":4,
              "COUNT":1
           }
        ],
        "SPEC_ID": "58411778"
     }
  ]
  </code></pre>
  
  <p>however, i was able to write the nested group by <strong>STATUS</strong> and <strong>ASSIGN_ID</strong>. unable to get the recent <strong>SPEC_ID</strong>. please find the query below.</p>
  
  <pre><code>            Modal.aggregate([
                {"$group": {
                  "_id": {
                      "INSPECTED_BY": "$INSPECTED_BY",
                      "STATUS": "$STATUS"
                  },
                  "total": { "$sum": 1 }
                }},
                {"$group": {
                  "_id": "$_id.INSPECTED_BY",
                  "data": { "$push": {
                      "STATUS": "$_id.STATUS",
                      "total": "$total"
                  }}
                }}]);
  </code></pre>
  
  <p>and it return a data set like below.</p>
  
  <pre><code>[  
     {  
        "ASSIGN_ID":"583f84bce58725f76b322398",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":3,
              "COUNT":1
           },
           {  
              "STATUS":4,
              "COUNT":2
           }
        ],
     },
     {  
        "ASSIGN_ID":"583f84bce58725f76b322311",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":3,
              "COUNT":1
           }
        ],
  
     },
     {  
        "ASSIGN_ID":"583f84bce58725f76b322322",
        "STATUS_GROUP":[  
           {  
              "STATUS":1,
              "COUNT":1
           },
           {  
              "STATUS":4,
              "COUNT":1
           }
        ],
     }
  ]
  </code></pre>