go语言Mongodb聚合统计每个(时间)段内的数据

根据(时间)段来聚合统计每个(时间)段内的数据

//每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条

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值