我想找到所有病历中cough
和headache
的病人
每个医疗记录都包含两个记录症状的字段,“SYMPTOM_1
,SYMPTOM_2
”
我通过以下查询查询,但是我收到了这个错误。
exception: aggregating group operators are unary ($push)
我希望在病历史上发现患者咳嗽和头痛
咳嗽代码:cough_codes = ["477 ","4770 "]
头痛的代码:headache_codes = ["147 ","1470 "]
cur= db_conn.medical_records.aggregate(
[
{"$match":{
"$and":[
{
"$or":[
{'SYMPTOM_1': '477 '},
{'SYMPTOM_2': '4770 '}
]
},
{
"$or":[
{'SYMPTOM_1': '147 '},
{'SYMPTOM_2': '1470 '}
]
},
]
}
},
{ "$sort": { "APPL_DATE": 1}},
{ "$group":
{
"_id": "$ID",
"dis": { "$push": ["$SYMPTOM_1","$SYMPTOM_2"] }
}
}
]
) # end of aggregate query
{
"id": 1,
"name": "Mary",
"SYMPTOM_1": "4770 ",
"SYMPTOM_2": "477 ",
"datetime": "2011-04-02"
},
{
"id": 2,
"name": "Jack",
"SYMPTOM_1": "477 ",
"SYMPTOM_2": "4770 ",
"datetime": "2011-04-02"
},
{
"id": 3,
"name": "Mark",
"SYMPTOM_1": "477 ",
"SYMPTOM_2": "4770 ",
"datetime": "2010-01-02"
},
....
{
"id": 4,
"name": "Jack",
"SYMPTOM_1": "477 ",
"SYMPTOM_2": "1470 ",
"datetime": "2015-04-09"
},
{
"id": 5,
"name": "Mary",
"SYMPTOM_1": "477 ",
"SYMPTOM_2": "147 ",
"datetime": "2010-09-02"
}
答案 0 :(得分:0)
您需要指定" _id"在mongo查询中对数据进行分组。
根据提供的示例数据,您可以尝试以下操作:
db.collection.aggregate(
[
{"$match":{
"$and":[
{
"$or":[
{'SYMPTOM_1': '477'},
{'SYMPTOM_2': '4770'}
]
},
{
"$or":[
{'SYMPTOM_1': '147'},
{'SYMPTOM_2': '1470'}
]
},
]
}
},
{ "$sort": { "APPL_DATE": 1}},
{ "$group":
{
"_id": "$_id",
"dis": { "$push": { SYMPTOM_1: "$SYMPTOM_1", SYMPTOM_2: "$SYMPTOM_2" }}
}
}
]
)
答案 1 :(得分:0)
您可以使用累加器:
Accumulators.push("dis",
new BasicDBObject("SYMPTOM_1", "$SYMPTOM_1")
.append("SYMPTOM_2", "$SYMPTOM_2"))