go - mongodb query using mgov2 -
i have collection of endpoint point tests conducted on various channels. sample document collection is:
{ "_id" : objectid("59959b30c699811077751b12"), "teststatus" : "fail", "channelname" : "housecontroller", "timestamp" : isodate("2017-08-17t13:15:53.170z"), "testid" : "llpkigifiiquqksapwnn" }
i quering project result this:
[ { "fail": 20, "success count": 30, "total": 50, "channel": "c3" }, ...
but getting wrong count success , fail rate. current query in golang looks like:
o1:= bson.m{ "$project" :bson.m{ "channel": "$channelname", "time":"$timestamp", "teststatus" : "$teststatus", "_id":1, }, } o2:= bson.m{ "$group" :bson.m{ "_id": "$channel", "success": bson.m{ "$sum":bson.m{ "$eq" :"teststatus","pass"}, }, "total": bson.m{ "$sum": 1, }, }, } o3:= bson.m{ "$project" :bson.m{ "channel": "$_id", "success count":"$success", "total" : "$total", "_id":0, "fail": bson.m{ "$subtract": []interface{}{"$total", "$success"}, }, }, }
i doing wrong in counting of success count. cant figure right. have started mgo , golang. in advance
you need use $cond
conditional counting. example following counts tests, failed ones , successful ones in 1 step:
o2 := bson.m{ "$group" :bson.m{ "_id": "$channel", "total": bson.m{ "$sum": 1, }, "success": bson.m{"$sum": bson.m{ "$cond": []interface{}{ bson.m{ "$eq": []interface{}{"$teststatus", "pass"}}, 1, 0, }, }}, "fail": bson.m{"$sum": bson.m{ "$cond": []interface{}{ bson.m{"$eq": []interface{}{"$teststatus", "fail"}}, 1, 0, }, }}, }, }
Comments
Post a Comment