I have MongoDB data of the following form:
{"_id":"53eb9a5673a57578a10074ec","data":{"statistics":{"gsm":[{"type":"Attacks","value":{"team1":66,"team2":67}},{"type":"Corners","value":{"team1":8,"team2":5}},{"type":"Dangerous attacks","value":{"team1":46,"team2":49}},{"type":"Fouls","value":{"team1":9,"team2":14}},{"type":"Free kicks","value":{"team1":18,"team2":10}},{"type":"Goals","value":{"team1":2,"team2":1}},{"type":"Goal kicks","value":{"team1":10,"team2":11}},{"type":"Offsides","value":{"team1":1,"team2":4}},{"type":"Posession","value":{"team1":55,"team2":45}},{"type":"Shots blocked","value":{"team1":4,"team2":1}},{"type":"Shots off target","value":{"team1":7,"team2":5}}]}}}
I want to get the average of data.statistics.gsm.value.team1 when data.statistics.gsm.type == "Attacks" using the Golang MongoDB driver mgo. Code I have tried so far (with either one or both the group statements below):
pipeline := []bson.M{
bson.M{"$match": bson.M{"kick_off.utc.gsm.date_time": bson.M{"$gt": start, "$lt": end}}},
bson.M{
"$group": bson.M{
"_id": "$gsm_id",
"event_array" : bson.M{"$first": "$data.statistics.gsm"}}},
bson.M{
"$group": bson.M{
"_id": "$type",
"avg_attack" : bson.M{"$avg": "$data.statistics.gsm.value.team1"}}}}
With only the first group statement, I get back the below, but the second group statement doesn't help me get the average.
[{"_id":1953009,"event_array":[{"type":"Attacks","value":{"team1":48,"team2":12}},{"type":"Corners","value":{"team1":12,"team2":0}},{"type":"Dangerous attacks","value":{"team1":46,"team2":7}},{"type":"Fouls","value":{"team1":10,"team2":3}},{"type":"Free kicks","value":{"team1":5,"team2":12}},{"type":"Goals","value":{"team1":8,"team2":0}}