go mongodb Pipeline 聚合查询格式

//类似于$sum的操作还有很多,比较常用的是$min(求最小值),$max(求最大值)  查询条件  match  和聚合 group 查询一定要注意顺序 
aggregate := mongo.Pipeline{ bson.D{ {"$match" ,bson.D{ { "season_id", seasonId}, {"data_source_at",bson.M{"$gte": start}}, {"data_source_at",bson.M{"$lte": end}}}, }, }, bson.D{ {"$group", bson.D{ {"_id", "$student_id"}, {"win_count", bson.D{ {"$sum", "$win_count"}, }}, {"school_id", bson.D{ {"$first", "$school_id"}, }}, }}, }, }

//另外一种写法

aggregate := mongo.Pipeline{
{
{"$match" ,bson.D{
{ "season_id", seasonId},
{"data_source_at",bson.M{"$gte": start}},
{"data_source_at",bson.M{"$lte": end}},
},
},
},
{
{"$group", bson.D{
{"_id", "$student_id"},
{"win_count", bson.D{
{"$sum", "$win_count"},
}},
{"up_star", bson.D{
{"$sum", "$up_star"},
}},
}},
},

}
posted @ 2022-05-19 11:16  hubb  阅读(249)  评论(0编辑  收藏  举报