复杂的聚合查询,请大牛指导

会飞的闪电狮子 2018-06-20 10:17:43
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有好几十个类似的,有没有什么简便的方法。需求就是查出满足众多条件中的任意一项的数据
...全文
266 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
邹小青 2018-12-26
  • 打赏
  • 举报
回复
《Mongo计算难点总结归纳》这篇文章有一部分内容是关于分组聚合的

1,747

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧