mongodb - 如何对第三层的字段进行aggregate的group聚合
世界只因有你
世界只因有你 2017-05-02 09:17:58
[MongoDB讨论组]

如下数据格式,我想对 fill_time 进行 group by,fill_time字段嵌套在第三层。
业务要求:如果这一条记录有多个不同的fill_time,则需分组统计多次。例如下面的数据,则统计两次,分别3月15日3月17日各算一个,请问能不能用 aggregate 实现?

{
    "_id": "56e932f42ec56a4afc00a083",
    "binding_time": "2016-03-16 18:18:28",
    "categorys": [
        {
            "category_code": "003",
            "category_name": "颈椎疾患",
            "datas": [
                {
                    "fill_time": "2016-03-17 10:47:59",
                    "form_code": "6",
                    "plan_code": "P45",
                    "results": []
                },
                {
                    "fill_time": "2016-03-17 10:48:25",
                    "form_code": "3",
                    "plan_code": "P45",
                    "results": []
                }
            ],
            "edc_patId": "P015830",
            "start_time": "2014-4-21 13:41:42"
        },
        {
            "category_code": "004",
            "category_name": "脊柱肿瘤",
            "datas": [
                {
                    "fill_time": "2016-03-15 10:48:48",
                    "form_code": "34",
                    "plan_code": "P47",
                    "results": []
                },
                {
                    "fill_time": "2016-03-15 10:49:14",
                    "form_code": "36",
                    "plan_code": "P47",
                    "results": []
                }
            ],
            "edc_patId": "P038894",
            "start_time": "2014-4-21 13:41:42"
        }
    ],
    "patient_id": "000997785203",
    "person_name": "张三",
    "project_code": "SYGK",
    "subscribe": false
}
世界只因有你
世界只因有你

全部回复(1)
PHP中文网
aggregate([
{$unwind:"$categorys"},
{$unwind:"$categorys.datas"},
{$project:{
        'date':{$substr : ["$categorys.datas.fill_time",0,10]}
    }
},
{$group:{
        _id:'$date',
        "count":{$sum:1}
    }
}])
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号