簡體   English   中英

使用 python 的動態 mongo 查詢

[英]Dynamic mongo query with python

我有一個 mongo 聚合查詢,我想在一些 json 數據的基礎上創建,所以查詢將像動態的,它將根據 json 選擇數組增長。

{ 
 RuleResultSUmmaryByCategoryRequest:{
      "topLevelFilterIds": [],
      "category": "abc",
      "selection": [{
        "value": "value",
        "category": "category"
      }]
    }
}

我的 mongo 聚合查詢就像

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.

我已經完成了動態查詢,現在我可以創建一個動態 mongo 聚合查詢。

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)

如果有人可以幫助我進行優化或任何更好的方式來實現相同的目標,那將是很大的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM