MongoDB 聚合+管道
来源:互联网 发布:淘宝卖家如何上传宝贝 编辑:程序博客网 时间:2024/06/07 03:25
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。有点类似sql语句中的count(*)。
语法
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
实例
集合中的数据格式:
{ "_id" : ObjectId("577e09da6a857a35563587c0"), "_class" : "com.market.salepromotionapi.coupon.pojo.CouponPojo", "couponGroupId" : "C201607070000000003", "couponGroupName" : "线线线线线线线线线线线线线线线", "description" : "string", "price" : 1.0000000000000000, "range" : [ { "_class" : "com.market.salepromotionapi.coupon.pojo.GoodsSKUPojo", "skuId" : "IMHNSKU20160818100188003", "skuName" : "2室2厅1卫_", "groupId" : "IMHNGROUP2016081810000188", "houseId" : "IMHN2016081810000188", "houseName" : "钱塘玫瑰湾", "developName" : "" }, { "_class" : "com.market.salepromotionapi.coupon.pojo.GoodsSKUPojo", "skuId" : "IMHNSKU20160818100188002", "skuName" : "2室2厅1卫_", "groupId" : "IMHNGROUP2016081810000188", "houseId" : "IMHN2016081810000188", "houseName" : "钱塘玫瑰湾", "developName" : "" } ], "type" : 0, "startTime" : ISODate("2016-07-07T15:50:50.000+08:00"), "endTime" : ISODate("2017-09-30T15:50:50.000+08:00"), "createTime" : ISODate("2016-07-07T15:50:50.570+08:00"), "status" : 5, "updateList" : [ { "beanPrice" : 0.0000000000000000, "logType" : 0, "status" : 0, "updateTime" : ISODate("2016-11-18T15:55:10.915+08:00"), "updater" : "xxxx", "remarks" : "强制过期" }, { "beanPrice" : 0.0000000000000000, "logType" : 0, "status" : 0, "updateTime" : ISODate("2016-11-18T15:55:10.915+08:00"), "updater" : "xxxxx", "remarks" : "强制过期" } ], "updateTime" : ISODate("2016-11-18T15:55:10.915+08:00"), "sumbmiter" : "提交人", "submitTime" : ISODate("2016-10-26T09:21:18.180+08:00"), "frontDisplay" : "1", "updater" : "xxxxx", "submiter" : null, "checkRecordList" : [ { "_id" : null, "checker" : "系统处理", "status" : 3, "checkTime" : ISODate("2016-10-26T09:13:06.186+08:00"), "remarks" : "该优惠券已过期,系统已处理!" }, { "_id" : null, "status" : 2, "checkTime" : ISODate("2016-10-26T09:21:48.732+08:00"), "remarks" : "" } ], "checker" : null, "checkTime" : ISODate("2016-10-26T09:21:48.732+08:00"), "forceEndTime" : ISODate("2016-11-18T15:55:10.915+08:00")}
现在我们通过以上集合计算每个每个提交人创建的文档数,使用aggregate()计算结果如下:
根据资源文档的排序获取最后一个文档数据
下表展示了一些聚合的表达式:
管道的概念
管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。
MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。
表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。
表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。
这里我们介绍一下聚合框架中常用的几个操作:
- $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
- $match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
- $limit:用来限制MongoDB聚合管道返回的文档数。
- $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
- $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
- $group:将集合中的文档分组,可用于统计结果。
- $sort:将输入文档排序后输出。
- $geoNear:输出接近某一地理位置的有序文档。
1、$project实例
db.article.aggregate( { $project : { title : 1 , author : 1 , }} );
这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:
db.article.aggregate( { $project : { _id : 0 , title : 1 , author : 1 }});
2.$match实例
db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ] );
$match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段$group管道操作符进行处理。
3.$skip实例
db.article.aggregate( { $skip : 5 });
经过$skip管道操作符处理后,前五个文档被"过滤"掉。
0 0
- Mongodb 聚合管道
- MongoDB 聚合+管道
- MongoDB aggregate 聚合管道
- mongodb聚合管道
- 【mongoDB实战】聚合管道--$unwind
- MongoDB:6-MongoDB的聚合和管道
- MongoDB 聚合管道(一)(Aggregation Pipeline)
- MongoDB 聚合管道(二)(Aggregation Pipeline)
- MongoDB Aggregation 聚合管道(Aggregation Pipeline)
- MongoDB 聚合管道(Aggregation Pipeline)
- MongoDB 聚合管道(一)(Aggregation Pipeline)
- MongoDB 聚合管道(二)(Aggregation Pipeline)
- ThinkPHP支持MongoDb Aggregate方法聚合管道
- MongoDB小结27 - 聚合管道【$project】
- MongoDB 聚合管道(Aggregation Pipeline)
- MongoDB小结28 - 聚合管道【$match】
- MongoDB小结29 - 聚合管道【$cond】
- MongoDB小结30 - 聚合管道【$limit】
- struct、class占用字节数
- App开发:模拟服务器数据接口 - MockApi
- 关于区块链,程序员需要了解些什么?
- (块级格式化范围)[DIV/CSS] BFC
- jquery 开始与结束方法 loading窗
- MongoDB 聚合+管道
- 实际开发中,实用的辅助iOS开发的工具
- ios通过代码创建按钮
- zTree的模糊搜索
- 开发者必知的git基本命令
- 【一生的计划】读书笔记
- 字符串的匹配
- Redis实用教程之一---安装Redis
- The file or directory to be published does not exist: /data/vendor/bower/jquery/dist