zoukankan      html  css  js  c++  java
  • golang+mongodb 怎么样实现聚合查询,按字段分组并查找出相应字段

    集合sales的定义如下:

    { "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
    { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
    { "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
    { "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
    { "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }

    聚合函数如下:

    db.sales.aggregate(
       [
          {
            $group : {
               _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
               totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
               averageQuantity: { $avg: "$quantity" },
               count: { $sum: 1 }
            }
          }
       ]
    )
    { "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 }
    { "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 }
    { "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 40, "averageQuantity" : 1.5, "count" : 2 }

    或者在mongochef中的json view如下:

    { 
        "_id" : {
            "month" : NumberInt(4), 
            "day" : NumberInt(4), 
            "year" : NumberInt(2014)
        }, 
        "totalPrice" : 200.0, 
        "averageQuantity" : 15.0, 
        "count" : NumberInt(2)
    }
    { 
        "_id" : {
            "month" : NumberInt(3), 
            "day" : NumberInt(15), 
            "year" : NumberInt(2014)
        }, 
        "totalPrice" : 50.0, 
        "averageQuantity" : 10.0, 
        "count" : NumberInt(1)
    }
    { 
        "_id" : {
            "month" : NumberInt(3), 
            "day" : NumberInt(1), 
            "year" : NumberInt(2014)
        }, 
        "totalPrice" : 40.0, 
        "averageQuantity" : 1.5, 
        "count" : NumberInt(2)
    }

    实例说明:

    1、

     _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },

    此处指定的“_id” 必需且唯一,意为根据所列的域(字段)进行分组。

    如果指定: _id : null

    则意为对所有行进行分组统计,

    例子如下:

    db.sales.aggregate(
       [
          {
            $group : {
               _id : null,
               totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
               averageQuantity: { $avg: "$quantity" },
               count: { $sum: 1 }
            }
          }
       ]
    )

    查询结果如下:

    { "_id" : null, "totalPrice" : 290, "averageQuantity" : 8.6, "count" : 5 }

    2、

    $sum:1

    表示每条记录按照数量1计数,

    如果设为2,则每条记录(文档、每行)按照数量2计数,然后 总数=计数*行数(记录数*每条记录的计数)。

    3、

    $month: "$date" 

    根据日期字段取该日期对应的月份。

  • 相关阅读:
    MongoDB性能分析
    MongoDB复制
    redis键管理
    MySQL集群架构-DRBD+headbeat +lvs+keepalived
    Spark-Core RDD转换算子-双Value型交互
    Spark-Core RDD转换算子-Value型
    Spark-Core RDD的创建
    Spark-Core RDD概述
    数仓理论
    flume 进阶
  • 原文地址:https://www.cnblogs.com/dfsxh/p/12875651.html
Copyright © 2011-2022 走看看