我有以下形式的MongoDB数据:
{"_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}}]}}}
我想得到data.statistics.gsm.type ==" Attacks"使用Golang MongoDB驱动程序mgo。到目前为止我已尝试过的代码(使用下面的一个或两个组语句):
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"}}}}
只有第一组声明,我回到下面,但第二组声明并没有帮助我获得平均值。
[{"_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}}
答案 0 :(得分:2)
我总是觉得获得json的漂亮打印视图很有帮助。以下是您从第一组声明中得到的结论:
[
{
"_id":1953009,
"event_array":[
{
"type":"Attacks",
"value":{
"team1":48,
"team2":12
}
},
{
"type":"Corners",
"value":{
"team1":12,
"team2":0
}
},
...
现在使用的第二个群组声明:
"$group": bson.M{
"_id": "$type",
"avg_attack" : bson.M{"$avg": "$data.statistics.gsm.value.team1"}
}
你试图在第一组声明的结果中取data.statistics.gsm.value.team1
的平均值,但在第一组声明的结果中不存在,所以当然它不会给你一个平均。
而不是你正在使用的方法,我建议调查$unwind
operator将数组分解为一组文档,然后你应该能够按照你想要的方式对它们进行分组这里有{$avg: "$value.team1"}
。
因此,用于生成聚合的整个管道将是:$match -> $group1 -> $unwind -> $group2
。请记住,管道的每个阶段都在对前一阶段生成的数据进行操作,这就是data.statistics.gsm.value.team1
部分不正确的原因。