2016-11-05 15 views
1

マイコレクションの構造に関する背景情報。 discriminatorKeyを使用して、すべて1つのコレクションに保存されたさまざまな種類の金融取引があります。ここに例があります:モンゴーズ集約クエリのグループ化の改善

"_id" : ObjectId("5816346ef201a84e17a84899"), 
    "accountUpdatedBy" : -9.95, 
    "transactionAmount" : 9.95, 
    "paidTo" : "Vimeo", 
    "expenseCategory" : "advertising", 
    "accountName" : "Bank Name", 
    "transactionDate" : ISODate("2016-08-31T00:00:00Z"), 
    "transactionID" : "", 
    "transactionComment" : "", 
    "transactionCategory" : "SelfEmploymentExpense", 
    "transactionFee" : 0, 
    "entryDate" : ISODate("2016-10-30T17:57:02.144Z"), 
    "__v" : 0 

私がする必要があるのは、すべての取引タイプを合計して、年と月でグループ化したものです。

[ { total: 0, 
    year: 2016, 
    month: 8, 
    transactionCategory: 'AccountUpdate' }, 
    { total:100, 
    year: 2016, 
    month: 8, 
    transactionCategory: 'Other' }, 
    { total: 100, 
    year: 2016, 
    month: 8, 
    transactionCategory: 'SelfEmploymentExpense' }, 
    { total: 100, 
    year: 2016, 
    month: 8, 
    transactionCategory: 'SelfEmploymentIncome' }, 
    { total: 0, 
    year: 2016, 
    month: 9, 
    transactionCategory: 'AccountUpdate' }, 
    { total: 100, 
    year: 2016, 
    month: 9, 
    transactionCategory: 'CreditCardPayment' }, 
    { total: 100, 
    year: 2016, 
    month: 9, 
    transactionCategory: 'Other' }, 
    { total: 100, 
    year: 2016, 
    month: 9, 
    transactionCategory: 'SelfEmploymentExpense' } ] 

きれいであることの問題:しかし、これはこのような結果を生成

Transaction.findTransactionCategoryTotalsByMonth = function() { 
return this 
    .aggregate([ 
     {$group: { 
      _id: {transactionCategory: "$transactionCategory", month: {$month: "$transactionDate"}, year: {$year: "$transactionDate"}}, 
      total: {$sum: "$transactionAmount"}, 
     }}, 
     {$project: { 
      year: "$_id.year", 
      month: "$_id.month", 
      transactionCategory: "$_id.transactionCategory", 
      total: "$total", 
      _id: false, 
     }} 
    ]) 
    .sort({year: 1, month: 1, transactionCategory: 1}); 
}; 

:ここではいくつかの同様の質問を見てみたら、これは私が作ってみた最適なクエリであります明らかです。理想的には、レスポンスの書式設定時に反復を減らすために、データを少し統合する方法を探したいと思います。

[{year: 2016, 
    SomeTransactionType: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100], // Amounts for each month in an array 
    OtherTransactionType: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]}] 

以上のネストと、ダウンオブジェクトの数を取得する他の方法:のように見える結果を終わるために巣を私にできるようになる何か。

アドバイスをいただきありがとうございます。私はJavaScriptとプログラミング全般について非常に新しいので、これが基本的なものであれば事前にお詫びしておきます。

答えて

0

このようなことを試すことができます。最初にグループ化して各月合計の合計を求め、次にソートし、2番目にグループ化して各カテゴリの月合計をプッシュし、最終的なグループ化を行って各年を合計します。

aggregate([{ 
     $group: { 
      _id: { 
       transactionCategory: "$transactionCategory", 
       month: { 
        $month: "$transactionDate" 
       }, 
       year: { 
        $year: "$transactionDate" 
       } 
      }, 
      monthTotal: { 
       $sum: "$transactionAmount" 
      } 
     } 
    }, { 
     $sort: { 
      year: 1, 
      month: 1, 
      transactionCategory: 1 
     } 
    }, { 
     $group: { 
      _id: { 
       transactionCategory: "$_id.transactionCategory", 
       year: "$_id.year" 
      }, 
      yearTotal: { 
       $push: "$monthTotal" 
      }, 
     } 
    }, { 
     $group: { 
      _id: { 
       year: "$_id.year" 
      }, 
      categoryTotal: { 
       $push: { 
        transactionCategory: "$_id.transactionCategory", 
        yearTotal: "$yearTotal" 
       } 
      } 
     } 
    }, { 
     $project: { 
      _id: 0, 
      year: "$_id.year", 
      categoryTotal: "$categoryTotal" 
     } 
    }]) 

サンプル出力

{ 
    "categoryTotal": [{ 
     "transactionCategory": "donating", 
     "yearTotal": [15.95] 
    }, { 
     "transactionCategory": "campaigning", 
     "yearTotal": [10.95] 
    }, { 
     "transactionCategory": "advertising", 
     "yearTotal": [12.95, 21.9] 
    }], 
    "year": 2016 
} 
0

はこの1

`

Transaction.findTransactionCategoryTotalsByMonth = function() { 
return this 
    .aggregate([ 
     {$group: { 
      _id: {year: {$year: "$transactionDate"},transactionCategory: "$transactionCategory"}, 
      total: {$push: "$transactionAmount"}, 
     }}, 
    {$group: { 
      _id: "_id.year", 
transactionCategoryArray : {$addToSet:{transactionCategory: "$_id.transactionCategory",transactionAmount:"$total"}} 
     }},{$sort:{_id:-1}} 
    ]) 
}; 

`

をお試しください