复杂的聚合查询,请大牛指导
exclusion1 = list(self.dw['CQM2018'].aggregate([
{'$match': {'_id': {'$in': denom_id}}},
{'$project': {'ClinicID': 1, 'PatientID': 1,
'Depression_diagnosis': 1, 'Bipolar_Diagnosis': 1, 'Depression_Screening_Encounter_Codes': 1}},
{'$unwind': {'path': '$Depression_diagnosis', 'preserveNullAndEmptyArrays': True}},
{'$unwind': {'path': '$Bipolar_Diagnosis', 'preserveNullAndEmptyArrays': True}},
{'$unwind': {'path': '$Depression_Screening_Encounter_Codes'}},
{'$project': {'ClinicID': 1, 'PatientID': 1,
'compare1': {'$cond': {'if': {'$gt': ['$Depression_diagnosis.DOS', None]}, 'then': {'$lte': ['$Depression_diagnosis.DOS', '$Depression_Screening_Encounter_Codes.DOS']}, 'else': False}},
'compare2': {'$cond': {'if': {'$gt': ['$Bipolar_Diagnosis.DOS', None]}, 'then': {'$lte': ['$Bipolar_Diagnosis.DOS', '$Depression_Screening_Encounter_Codes.DOS']}, 'else': False}},
}},
{'$match': {'$or': [{'compare1': True}, {'compare2': True}]}},
]))
如果这里的compare1、compare2有好几十个类似的,有没有什么简便的方法。需求就是查出满足众多条件中的任意一项的数据