mongodb - 如何对第三层的字段进行aggregate的group聚合
世界只因有你
世界只因有你 2017-05-02 09:17:58
0
1
678

如下数据格式,我想对 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
}
世界只因有你
世界只因有你

Antworte allen(1)
Ty80
aggregate([
{$unwind:"$categorys"},
{$unwind:"$categorys.datas"},
{$project:{
        'date':{$substr : ["$categorys.datas.fill_time",0,10]}
    }
},
{$group:{
        _id:'$date',
        "count":{$sum:1}
    }
}])
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage