python - 多个集合中的聚合查询 Mongodb (pymongo)
问题描述
我想在多个集合中应用聚合查询。我有以下集合,每个集合中都有一个字段partner
,我想从partner
包含的所有集合中过滤掉john
,这是我的集合 -
公司
{
'com_id': 121,
'name': 'abc',
'pin': 45009,
'partner': ['john', 'mack', 'michel'],
'type': 'company'
}
风险投资
{
'ven_id': 342,
'type': 'real_estate',
'partner': ['john', 'duke', 'michel']
}
财产
{
'prop_id': 213,
'address': 'xyz',
'partner': ['john', 'gore', 'tiger'],
'type': 'property'
}
并希望得到预期的结果——
[
{'type': 'company', 'id': 121, 'name': 'john'},
{'type': 'real_estate', 'id': 342, 'name': 'john'},
{'type': 'property', 'id': 213, 'name': 'john'}
]
这是我尝试过的,但对我不起作用。
mydb.company.aggregate([
{'$match': {'partner': {'$in': ['john']}}},
{'$lookup': {'from': "ventures", 'localField': "partner", 'foreignField': "partner", 'as': "final_data"}},
{'$lookup': {'from': "property", 'localField': "partner", 'foreignField': "partner", 'as': "final_data"}},
])
解决方案
推荐阅读
- javascript - 对一组对象执行一些检查,如果其中一个条件匹配,则停止批量请求
- reactjs - React history.push 自动重定向
- image - 如何应用非最大抑制进行角点检测
- c - 哈希表未在 C 中填充
- android - Android BLE 未显示结果
- node.js - “重新安装 nodejs 后出现错误”
- c++ - C++ 导入关键字、模块关键字和导出关键字
- java - BottomNavigationActivity 中的片段变形
- java - 如何为 java jar 文件创建 Windows 安装程序?
- html - top/bottom:[%] 基于父元素的边框而不是高度