MongoDB $group
来源:互联网 发布:去日本带mac好吗 编辑:程序博客网 时间:2024/05/23 00:32
1.操作的表 (Collection) 的数据
{ "_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 by year&month&day - 通过年月日来分组统计
db.sales.aggregate(
[
{
[
{
$group : {
_id :{ month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" }}, #根据年月日统计
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } }, #totalPrice = (price_1 * quantity_1) + (price_2 * quantity_2) + . . .
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 }
0 0
- MongoDB $group
- mongodb group
- Mongodb的group
- mongoDB中的group
- MongoDB中的Group By
- mongoDB 中的group
- mongodb group分组
- mongodb中的group功能
- mongodb count,distinct,group
- MongoDB 聚合Group(一)
- MongoDB分组group
- mongodb group 查询
- MongoDB的group分组
- mongodb group操作
- Java 实现MongoDB Group 操作
- 关于MongoDB的group分组
- Mongodb java group by 查询
- 关于MongoDB的group用法
- 每个程序员需掌握的20个代码命名规则
- 数组与列表的相互转化
- 页面添加H5声音图标
- vlc播放实时流长时间运行问题解决方案
- 素数筛选 素数分解
- MongoDB $group
- Android-Notification(通知)
- Ubuntu16.04安装tomcat
- JDBC源码分析(1)
- HeadFirst Java学习笔记——类间交互
- 定义结构体时变量名后的冒号和数字
- 我理解的动态绑定
- JPA学习笔记(二)
- Mybatis最入门---代码自动生成(generatorConfig.xml配置)