MongoDB中聚合(aggregate)的使用

来源:互联网 发布:网络教育专业 编辑:程序博客网 时间:2024/05/19 09:15

场景描述: 查看下面json数据格式,我需要查询

code=["doctor_name","doctor_hospital","doctor_phone","doctor_order_count","doctor_channel","order_patient_phone","order_hospital"

并且时间范围在

long_time > 2016-10-17 17:09:32 && long_time < 2016-11-1 17:09:32

在前面的条件成立之后对他们进行分组,使用operation_id,code进行分组,并且计算分组后每组的count(*),并返回每个分组之后的组里面的集合

{    "_id" : ObjectId("5804954c87a9f8271edf184e"),    "operation_type" : "User",    "operation_id" : 5,    "operation_name" : "liuyk",    "code" : "toolbar_menu_doctor",    "event" : "toolbar_menu",    "remark" : "点击了导航栏医生按钮",    "long_time" : "2016-10-17 17:09:32",    "short_time" : "2016-10-17",    "time_key" : 1476695372},{    "_id" : ObjectId("5804955287a9f8271edf184f"),    "operation_type" : "User",    "operation_id" : 5,    "operation_name" : "liuyk",    "code" : "doctor_hospital",    "event" : "manage_doctors_index",    "remark" : "点击了查看医生医院按钮(医生为汪世源,id为690)",    "long_time" : "2016-10-17 17:09:38",    "short_time" : "2016-10-17",    "time_key" : 1476695378},{    "_id" : ObjectId("5804956687a9f8271edf1850"),    "operation_type" : "User",    "operation_id" : 5,    "operation_name" : "liuyk",    "code" : "doctor_orders",    "event" : "manage_doctors_index",    "remark" : "点击了查看订单按钮(医生为玉奎,id为691)",    "long_time" : "2016-10-17 17:09:58",    "short_time" : "2016-10-17",    "time_key" : 1476695398},{    "_id" : ObjectId("58049a8c87a9f8271edf1851"),    "operation_type" : "User",    "operation_id" : 37,    "operation_name" : "wangshiyuan",    "code" : "toolbar_menu_doctor",    "event" : "toolbar_menu",    "remark" : "点击了导航栏医生按钮",    "long_time" : "2016-10-17 17:31:56",    "short_time" : "2016-10-17",    "time_key" : 1476696716},{    "_id" : ObjectId("58049a9087a9f8271edf1852"),    "operation_type" : "User",    "operation_id" : 37,    "operation_name" : "wangshiyuan",    "code" : "toolbar_menu_doctor",    "event" : "toolbar_menu",    "remark" : "点击了导航栏医生按钮",    "long_time" : "2016-10-17 17:32:00",    "short_time" : "2016-10-17",    "time_key" : 1476696720}

查询与法(注意我这个是ruby与法哦,mongdb原声与法也差不多是这么写,改点东西就行啦)

txdiag_logs = TxdiagLog.collection.aggregate([{'$match' => { "code" => {"$in"=>["doctor_name","doctor_hospital","doctor_phone","doctor_order_count","doctor_channel","order_patient_phone","order_hospital"]},"long_time" => { "$gt"=>"#{Time.now.at_beginning_of_day-30.day}", "$lte"=>"#{Time.now.at_beginning_of_day}" } } },{'$group' => {'_id' => {"operation_id"=>"$operation_id","code"=>"$code"},"txdiag_logs"=> { "$push"=> "$$ROOT" },'_count'=> {"$sum":1}}}])

结果格式如下

[{    "_id": {        "operation_id": 9,         "code": "order_patient_phone"    },     "txdiag_logs": [        {            "_id": {                "$oid": "580dd1bb87a9f817f7111be3"            },             "operation_type": "User",             "operation_id": 9,             "operation_name": "guoyj",             "code": "order_patient_phone",             "event": "manage_orders_index",             "remark": "点击了查看患者电话按钮(订单编号为19025148,id为2515,患者电话为15548856885)",             "long_time": "2016-10-24 17:17:47",             "short_time": "2016-10-24",             "time_key": "1477300667317_6095"        }    ],     "_count": 1},{    "_id": {        "operation_id": 5,         "code": "doctor_channel"    },     "txdiag_logs": [        {            "_id": {                "$oid": "580853d087a9f841b89336e4"            },             "operation_type": "User",             "operation_id": 5,             "operation_name": "liuyk",             "code": "doctor_channel",             "event": "manage_doctors_index",             "remark": "点击了支付方式详情按钮(医生为hudi,id为695)",             "long_time": "2016-10-20 13:19:12",             "short_time": "2016-10-20",             "time_key": 1476940752        }    ],     "_count": 1}]
0 0
原创粉丝点击