mongodb - Aggregate with count of sub documents matching the condition and grouping -
i've collections of documents below:
{ "_id" : objectid("55d4410544c96d6f6578f893"), "executionproject" : "project1", "suitelist" : [ { "suitestatus" : "pass" } ], "runendtime" : isodate("2015-08-19t08:40:47.049z"), "runstarttime" : isodate("2015-08-19t08:40:37.621z"), "runstatus" : "pass", "__v" : 1 } { "_id" : objectid("55d44eb4c0422e7b8bffe76b"), "executionproject" : "project1", "suitelist" : [ { "suitestatus" : "pass" } ], "runendtime" : isodate("2015-08-19t09:39:13.528z"), "runstarttime" : isodate("2015-08-19t09:39:00.406z"), "runstatus" : "pass", "__v" : 1 } { "_id" : objectid("55d44f0bc0422e7b8bffe76f"), "executionproject" : "project1", "suitelist" : [ { "suitestatus" : "fail" } ], "runendtime" : isodate("2015-08-19t09:46:31.108z"), "runstarttime" : isodate("2015-08-19t09:40:27.377z"), "runstatus" : "pass", "__v" : 1 } { "_id" : objectid("55d463d0c0422e7b8bffe789"), "executionproject" : "project2", "suitelist" : [ { "suitestatus" : "pass" }, { "suitestatus" : "pass" } ], "runendtime" : isodate("2015-08-19t11:09:52.537z"), "runstarttime" : isodate("2015-08-19t11:09:04.539z"), "runstatus" : "fail", "__v" : 1 } { "_id" : objectid("55d464ebc0422e7b8bffe7c2"), "executionproject" : "project3", "suitelist" : [ { "suitestatus" : "fail" } ], "runendtime" : isodate("2015-08-19t11:18:41.460z"), "runstarttime" : isodate("2015-08-19t11:13:47.268z"), "runstatus" : "fail", "__v" : 10 }
and i'm expecting output follows:
[ { "executionproject": "project1", "suite-pass": 0, "suite-fail": 1, "runendtime": isodate("2015-08-19t09:46:31.108z") }, { "executionproject": "project2", "suite-pass": 2, "suite-fail": 0, "runendtime": isodate("2015-08-19t11:09:52.537z") }, { "executionproject": "project3", "suite-pass": 0, "suite-fail": 1, "runendtime": isodate("2015-08-19t11:18:41.460z") }, ]
i want group project , order runendtime , show pass , fail counts of suitelist.
i tried suggested blakes in mongodb: group element , show sub-document count based on condition , sort document date:
db.testruns.aggregate([ { "$sort": { "runendtime": 1 } }, { "$group": { "_id": "$executionproject", "suite-pass": { "$last": { "$cond": [ { "$anyelementtrue": { "$map": { "input": "$suitelist", "as": "suite", "in": { "$eq": [ "$$suite.suitestatus", "pass" ] } } }}, 1, 0 ] } }, "suite-fail": { "$last": { "$cond": [ { "$anyelementtrue": { "$map": { "input": "$suitelist", "as": "suite", "in": { "$eq": [ "$$suite.suitestatus", "fail" ] } } }}, 1, 0 ] } }, "runendtime": { "$last": "$runendtime" } }}, { "$sort": { "runendtime": 1 } } ]);
i expecting suite-pass
count project2
2 since there 2 elements in suitelist, returns 1.
you should have read answer properly, there alternate listing , explanation of why expected result want 1 used different.
instead want one, respects possible multiple "pass" or "fail":
model.aggregate( [ { "$sort": { "executionproject": 1, "runendtime": 1 } }, { "$group": { "_id": "$executionproject", "suitelist": { "$last": "$suitelist" }, "runendtime": { "$last": "$runendtime" } }}, { "$unwind": "$suitelist" }, { "$group": { "_id": "$_id", "suite-pass": { "$sum": { "$cond": [ { "$eq": [ "$suitelist.suitestatus", "pass" ] }, 1, 0 ] } }, "suite-fail": { "$sum": { "$cond": [ { "$eq": [ "$suitelist.suitestatus", "fail" ] }, 1, 0 ] } }, "runendtime": {"$first": "$runendtime"} }}, { "$sort": { "runendtime": 1 }} ], function(err,result) { } );
which sort of "combination" of approaches. first "last" runtime expecting. next break down array , time "sum up" possible occurances of pass or fail, rather record 1
either pass or fail in array, actual "pass" or "fail" counted.
with results:
{ "_id" : "project1", "suite-pass" : 0, "suite-fail" : 1, "runendtime" : isodate("2015-08-19t09:46:31.108z") } { "_id" : "project2", "suite-pass" : 2, "suite-fail" : 0, "runendtime" : isodate("2015-08-19t11:09:52.537z") } { "_id" : "project3", "suite-pass" : 0, "suite-fail" : 1, "runendtime" : isodate("2015-08-19t11:18:41.460z") }
Comments
Post a Comment