我有一个引用另一个文档的文档,我想加入这些文档并根据子文档中数组的内容进行过滤:
部署机器文档:
{
"_id": 1,
"name": "Test Machine",
"machine_status": 10,
"active": true
}
machine_status文件:
{
"_id": 10,
"breakdown": [
{
"status_name": "Rollout",
"state": "complete"
},
{
"status_name": "Deploying",
"state": "complete"
}
]
}
[我正在使用Mongo 3.6,并且在查找和管道方面都取得了成功,这是我在python MongoEngine中使用的对象传递给了聚合函数:
pipeline = [
{'$match': {'breakdown': {'$elemMatch': {'status_name': 'Rollout'}}}},
{'$lookup':
{
'from': 'deployment_machine',
'let': {'status_id': '$_id'},
'pipeline': [
{'$match':
{'$expr':
{'$and': [
{'$eq': ['$machine_status', '$$status_id']},
]},
}
}
],
'as': 'result',
},
},
{'$project': {
'breakdown': {'$filter': {
'input': '$breakdown',
'as': 'breakdown',
'cond': {'$eq': ['$$breakdown.status_name', 'Rollout']}
}}
}},
]
result = list(MachineStatus.objects.aggregate(*pipeline))
这很好,但是如果部署机器不活动,如何排除结果?我认为它必须在项目中进行,但找不到有效的条件。任何帮助表示赞赏。
您可以在$lookup
管道中添加更多条件