我正在尝试基于诊断类型并在更高级别patientKey
和encounterKey
上聚合文档。这是文档的样子:
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "medical_history",
"diagnosisCode" : "Z87.81",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 2,
"diagnosisType" : "problem_list",
"diagnosisCode" : "2x.2",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "D21.01",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "medical_history",
"diagnosisCode" : "X2.31",
}
{
"_id" : ObjectId("...."),
"patientKey" : 1,
"encounterKey" : 3,
"diagnosisType" : "problem_list",
"diagnosisCode" : "p.2342",
}
这是我希望聚合显示的样子:
{
"patientKey": 1,
"encounters":[{
encounterKey: 2,
medical_history: [Z87.81],
problem_list: [2x.2]
},
{
encounterKey: 3,
medical_history: [D21.01, X2.31],
problem_list: [p.2342]
}]
}
有什么办法可以解决这个问题吗?
我尝试过以下操作以获取病历,然后再列出问题列表,最初的想法是先在“问题列表”上汇总,然后再在“病历”上汇总,然后合并集合,然后根据遇到的情况再进行汇总,最后再进行汇总在耐心的。
但是合并异类文档是一个问题。
db.collections.aggregate([
{$match:{diagnosisType:"Problem List"}},
{$group:{_id:"$encounterKey",
"ProblemList": {$push:{$concat:"$diagnosisCode"}},
"durableKey":{$first:"$durableKey"}}},
{$project:{_id:0,
encounterKey:"$_id",
ProblemList:1,
durableKey:1}},
{$out:"output"}
],{
allowDiskUse: true
})
答案 0 :(得分:1)
您可以使用以下汇总:
db.col.aggregate([
{
$group: {
_id: { patientKey: "$patientKey", encounterKey: "$encounterKey", diagnosisType: "$diagnosisType" },
problem_list: { $push: "$diagnosisCode" }
}
},
{
$group: {
_id: { patientKey: "$_id.patientKey", encounterKey: "$_id.encounterKey" },
encounters: { $push: { k: "$_id.diagnosisType", v: "$problem_list" } }
}
},
{
$project: {
encounters: { $arrayToObject: "$encounters" }
}
},
{
$group: {
_id: "$_id.patientKey",
encounters: {
$push: {
encounterKey: "$_id.encounterKey",
problem_list: "$encounters.problem_list",
medical_history: "$encounters.medical_history"
}
}
}
},
{
$project: {
patientKey: "$_id",
_id: 0,
encounters: 1
}
}
])
基本上,您需要几个$group阶段来累积数据。另外,您必须使用$arrayToObject动态构建JSON密钥。
输出:
{
"patientKey" : 1,
"encounters" : [
{
"encounterKey" : 2,
"problem_list" : [
"2x.2"
],
"medical_history" : [
"Z87.81"
]
},
{
"encounterKey" : 3,
"problem_list" : [
"p.2342"
],
"medical_history" : [
"D21.01",
"X2.31"
]
}
]
}