Reputation: 159
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.
Upvotes: 2
Views: 811
Reputation: 159
I am done with dynamic query and now i am able to create a dynamic mongo aggregate query.
pipeline = []
matches = {}
matches["$and"]=[]
matches["$and"].append({ "topLevelFilter.id": { "$in": request_data["topLevelFilterIds"]}})
for x in request_data['Filter']:
matches["$and"].append({x['category']: x['value']})
pipeline.append({'$match': matches})
first_stage_grouping = {}
first_stage_grouping = {'_id': {'groupByColumn': '$' + request_data['category'],
'topLevelFilter': '$topLevelFilter.id'}}
first_stage_grouping['name'] = {'$first': '$topLevelFilter.name'}
first_stage_grouping['type'] = {'$first': '$topLevelFilter.type'}
first_stage_grouping['fail'] = {
'$sum': {'$cond': [{'$eq': ['$summaryStatus', 0]}, 1, 0]}}
first_stage_grouping['pass'] = {
'$sum': {'$cond': [{'$eq': ['$summaryStatus', 1]}, 1, 0]}}
first_stage_grouping['warn'] = {
'$sum': {'$cond': [{'$eq': ['$summaryStatus', 2]}, 1, 0]}}
pipeline.append({'$group': first_stage_grouping})
second_stage_grouping = {}
second_stage_grouping = {'_id': '$_id.groupByColumn'}
second_stage_grouping['fail'] = {'$sum': '$fail'}
second_stage_grouping['pass'] = {'$sum': '$pass'}
second_stage_grouping['warn'] = {'$sum': '$warn'}
second_stage_grouping['portfolio'] = {'$push': {'id': '$_id.topLevelFilter',
'name': '$name', 'type': '$type', 'fail': '$fail', 'pass': '$pass', 'warn': '$warn'}}
pipeline.append({'$group': second_stage_grouping})
third_stage_grouping = {}
third_stage_grouping = {'_id': 0}
third_stage_grouping['result'] = {'$push': {
'category': '$_id', 'fail': '$fail', 'pass': '$pass', 'warn': '$warn', 'portfolio': '$portfolio'}}
third_stage_grouping['fail'] = {'$sum': '$fail'}
third_stage_grouping['pass'] = {'$sum': '$pass'}
third_stage_grouping['warn'] = {'$sum': '$warn'}
pipeline.append({'$group': third_stage_grouping})
projection = {}
projection = {'_id': 0, 'data': '$result', 'total': {
'fail': '$fail', 'pass': '$pass', 'warn': '$warn'}}
pipeline.append({'$project': projection})
print(pipeline)
If anyone can help me to optimize or any better way to achive the same, it will be great help.
Upvotes: 1