I need max date for SUCCESS status for each mid .Below is the document structure. I tried using aggregate but unble to get the required result.
{"mid":1,"date":ISODate("2015-06-07T00:00:00Z"),"status":"FAILED"}
{"mid":1,"date":ISODate("2015-06-04T00:00:00Z"),"status":"FAILED"}
{"mid":1,"date":ISODate("2015-06-01T00:00:00Z"),"status":"SUCCESS"}
{"mid":1,"date":ISODate("2015-05-27T00:00:00Z"),"status":"SUCCESS"}
{"mid":2,"date":ISODate("2015-05-17T00:00:00Z"),"status":"SUCCESS"}
{"mid":2,"date":ISODate("2015-05-07T00:00:00Z"),"status":"FAILED"}
{"mid":3,"date":ISODate("2015-05-04T00:00:00Z"),"status":"FAILED"}
{"mid":3,"date":ISODate("2015-05-03T00:00:00Z"),"status":"FAILED"}
{"mid":3,"date":ISODate("2015-05-02T00:00:00Z"),"status":"SUCCESS"}
{"mid":3,"date":ISODate("2015-05-01T00:00:00Z"),"status":"FAILED"}
{"mid":4,"date":ISODate("2015-04-07T00:00:00Z"),"status":"FAILED"}
{"mid":4,"date":ISODate("2015-04-01T00:00:00Z"),"status":"SUCCESS"}
Use the following aggregation pipeline:
Sample Output:
-- EDIT --
To include the counts for the status
SUCCESSandFAILEDfor eachmid, you need to change the$matchquery to use an$inoperator with those two statuses, the$groupby identifiers would change as well to group the documents by the mid and status fields and you can then add the count by applying the accumulator operator$sumon the group:Sample Output