使用MongoDB管道数组进行聚合

时间:2016-03-01 08:55:06

标签: mongodb

假设我有一个像这样的数组

pipeline = [
  {
    "$group": {
      "_id": {
        "cohort_name": "$CohortMonthYear",
        "segment_name": "$SegmentName",
        "driver_name": "Operating Expense"
      },
      "cohort_list": {
        "$push": {
          "driver_value": "$Operating Expense",
          "mob_value": "$MonthsOnBooks"
        }
      }
    }
  },
  {
    "$group": {
      "_id": {
        "cohort_name": "$CohortMonthYear",
        "segment_name": "$SegmentName",
        "driver_name": "Other fee per active"
      },
      "cohort_list": {
        "$push": {
          "driver_value": "$Other fee per active",
          "mob_value": "$MonthsOnBooks"
        }
      }
    }
  }
]

我想通过数组中的每个管道聚合Collection。如何在不需要迭代数组然后聚合单个管道的情况下执行此操作?

收集架构:

{
    "_id" : ObjectId("56cea0de29beab9b1128142e"),
    "SegmentName" : "Transactor-High-Super Prime",
    "Segment_Behaviour" : "Transactor",
    "Segment_Usage" : "High",
    "Segment_Risk" : "Super Prime",
    "CohortMonthYear" : "01-01-2010",
    "CohortQuarterYear" : "Q1 2010",
    "MonthsOnBooks" : 1,
    "Accounts" : 5400,
    "Active rate" : 0.6102349013766621,
    "Purchase per active" : 489.9747013271109495,
    "BT per active" : 270.3482080015934343,
    "CA per active" : 1.0073548882354659,
    "AMF per active" : 0.0046451682730326,
    "Late and Overlimit fee per active" : 0.0032395984480145,
    "Other fee per active" : 7.9199489691490577,
    "Purchase APR" : 0,
    "BT APR" : 0.0300000000000000,
    "Cash APR" : 0.1763020000000000,
    "Interchange Rate" : 0.0142100000000000,
    "Payment Rate" : 0,
    "BT Fee Rate" : 0.0291840000000000,
    "Cash advance fee rate" : 0.0328347765703207,
    "Unit loss rate" : 0.0000000000000000,
    "Balance Control Ratio" : 2.2999868799572565,
    "Cost of Funds" : 0.0281148836230321,
    "Marketing Cost" : 125.6961196036371575,
    "Operating Expense" : 0.8725128657629959,
    "Yield" : 0.0000000000000000,
    "ADB" : 12.3296052310066617,
    "portfolio_id" : ObjectId("56ce9f92c95a8a459ba0c580")
}

我目前的方法概述如下。这种方法的问题是它非常慢。

for template in pipeline:
    PortfolioData.objects(portfolio_id=self.portfolio_id).aggregate(template,
                                                                             allowDiskUse=True)

有没有办法做这样的事情?

PortfolioData.objects(portfolio_id=self.portfolio_id).aggregate(pipeline,
                                                                              allowDiskUse=True)

0 个答案:

没有答案
相关问题