美文网首页
Mongo Aggregate

Mongo Aggregate

作者: 海岸taurus | 来源:发表于2016-09-14 11:39 被阅读0次

    按照日期Group的例子

    1. 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")}

    2. 按照年月日Group

    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}}
     }])

    3. 结果如下

    {"_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}

    官网图

    1. MapReduce:

    2. Aggregate:




    相关文章

      网友评论

          本文标题:Mongo Aggregate

          本文链接:https://www.haomeiwen.com/subject/ugsvettx.html