如何首先将以下文件分组为" patient_group"然后通过"神经时间点"找到" hricph"?
的平均值{ "cases" : { "neuro" : { "neuro-time-point" : "0-12", "hricph" : 1 }, "patient_group" : "HSD" } }
{ "cases" : { "neuro" : { "neuro-time-point" : "12-24", "hricph" : 2 }, "patient_group" : "HSD" } }
{ "cases" : { "neuro" : { "neuro-time-point" : "24-36", "hricph" : 3 }, "patient_group" : "HSD" } }
{ "cases" : { "neuro" : { "neuro-time-point" : "0-12", "hricph" : 1 }, "patient_group" : "HSD" } }
{ "cases" : { "neuro" : { "neuro-time-point" : "24-36", "hricph" : 5 }, "patient_group" : "HSD" } }
{ "cases" : { "neuro" : { "neuro-time-point" : "36-48", "hricph" : 5 }, "patient_group" : "HSD" } }
期望的结果(或接近此的结果,以不同患者群体呈现多线图的格式):
{ "patient_group" : "HSD",
"avg_hricph_val" :
{
"neuro_time_point" : "0-12",
"hricph" : 1
},
{
"neuro_time_point" : "12-24",
"hricph" : 2
},
{
"neuro_time_point" : "24-36",
"hricph" : 4
},
{
"neuro_time_point" : "36-48",
"hricph" : 5
}
}
以下是我的尝试:
db.test_collection.aggregate([ { "$group": {
"_id": {
"patient_group": "$cases.patient_group",
"neuro_time_point" : "$cases.neuro.neuro-time-point",
"hricph" : "$cases.neuro.hricph"
},
"avg_hricph_val": { "$avg": 1 }
}}
])
答案 0 :(得分:0)
这个怎么样:
db.test_collection.aggregate(
[
{
"$group": {
"_id": { "id": "$cases.patient_group", "neuro_time_point": "$cases.neuro.neuro-time-point" },
"hricph": { "$avg": "$cases.neuro.hricph" }
}
},
{
"$group": {
"_id": null, "patient_group": { "$first": "$_id.id" },
"avg_hricph_val": { "$addToSet": { "neuro_time_point": "$_id.neuro_time_point", "hricph": "$hricph" }}
}
},
{
"$project": { "patient_group": 1, "_id": 0, "avg_hricph_val": 1 }
}
]
)
<强>结果强>
{
"patient_group" : "HSD",
"avg_hricph_val" : [
{
"neuro_time_point" : "0-12",
"hricph" : 1
},
{
"neuro_time_point" : "12-24",
"hricph" : 2
},
{
"neuro_time_point" : "24-36",
"hricph" : 4
},
{
"neuro_time_point" : "36-48",
"hricph" : 5
}
]
}