根据(时间)段来聚合统计每个(时间)段内的数据
//每200范围的高度数据统计
client := global.GvaMongo.Database(global.GvaConfig.MongoDB.Dbname).Collection(global.Collection)
groupStage :=bson.D{
{"$group", bson.D{
{"_id", bson.D{
//{"lei_xing", "$lei_xing"},
//{"xing_hao", "$xing_hao"},
//{"pi_hao", "$pi_hao"},
{"$subtract", bson.A{
bson.M{"$subtract":bson.A{bson.D{{"$convert",bson.D{{"input","$gao_du"},{"to","double"},{"onError","高度数据有误"},{"onNull",0}}}},200}},
bson.M{ "$mod": bson.A{bson.M{ "$subtract": bson.A{ bson.D{{"$convert",bson.D{{"input","$gao_du"},{"to","double"},{"onError","高度数据有误"},{"onNull",0}}}},200 } },200}},
},
},
},
},
{"count",bson.M{"$sum":1}},
},
},
}
//var matchCondition = []bson.E{}
var matchCondition = bson.M{}
matchStage :=bson.D{{"$match",matchCondition}}
cur,_:= client.Aggregate(context.TODO(),mongo.Pipeline{matchStage,groupStage})
// Get a list of all returned documents and print them out. // See the mongo.Cursor documentation for more examples of using cursors.
var results []map[string]interface{}
if err = cur.All(context.TODO(), &results);
err != nil { log.Fatal(err) }
运行结果:
结果表示:根据类型和型号分类 1000-1200高度的数据有1632条,4000-4200的数据有2052条