I have a mongo aggregation query that i want to create on the basis of some json data, So the query will be like dynamic and it will grow as per json selection array.
{
RuleResultSUmmaryByCategoryRequest:{
"topLevelFilterIds": [],
"category": "abc",
"selection": [{
"value": "value",
"category": "category"
}]
}
}
my mongo aggregate query is like
db.rule_execution_result.aggregate([{
$match: {
$and: [{
'topLevelFilter.id': {
$in: ['5fd1bd7868d7ac4e211a7642']
}
},
{
'ruleCategory': 'test'
}
]
}
},
{
$group: {
_id: {
ruleCategory: '$ruleCategory',
topLevelFilter: '$topLevelFilter.id'
},
name: {
$first: '$topLevelFilter.name'
},
type: {
$first: '$topLevelFilter.type'
},
fail: {
$sum: {
$cond: [{
$eq: ['$summaryStatus', 0]
}, 1, 0]
}
}
}
},
{
$group: {
_id: '$_id.ruleCategory',
fail: {
$sum: '$fail'
},
portfolio: {
$push: {
id: '$_id.topLevelFilter',
name: '$name',
type: '$type',
fail: '$fail'
}
}
}
},
{
$group: {
_id: 0,
result: {
$push: {
category: '$_id',
fail: '$fail',
portfolio: '$portfolio'
}
},
fail: {
$sum: '$fail'
}
}
},
{
$project: {
_id: 0,
data: '$result',
total: {
fail: '$fail'
}
}
}
]).pretty()
Here i need to use loop on json selection request object that i am getting from API and genrate dynamic stages for match and group, I am new in python so can any one help me to write it in dynamic way in python.