2016-08-02 14 views
1

私は集計スクリプトを用意しています。この集計スクリプトは、最終段階で次の結果を出力します。私は現在、アカウントのカテゴリ別にグループ化して合計したいと思っていますが、私は何とかBSONタイプのEOOをDateに変換しようとしています。MongoDbグループ月間合計集計を実行できません

{ 
"_id" : { 
    "party_uuid" : "phildominickcompany", 
    "connection_uuid" : "5738fc661a21db15b5c45b49", 
    "account_balances_date" : ISODate("2016-06-30T10:00:00.000+0000"), 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"account_identifier" : "5010", 
"account_name" : "Cost of sales - materials", 
"account_category" : "Sales Expense", 
"account_type" : null, 
"account_value_type" : "debit", 
"account_value" : NumberInt(0) 
} 
{ 
"_id" : { 
    "party_uuid" : "phildominickcompany", 
    "connection_uuid" : "5738fc661a21db15b5c45b49", 
    "account_balances_date" : ISODate("2016-07-31T10:00:00.000+0000"), 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"account_identifier" : "4000", 
"account_name" : "Sales Type A", 
"account_category" : "Sales Revenue", 
"account_type" : null, 
"account_value_type" : "credit", 
"account_value" : 57728.33 
} 
{ 
"_id" : { 
    "party_uuid" : "phildominickcompany", 
    "connection_uuid" : "5738fc661a21db15b5c45b49", 
    "account_balances_date" : ISODate("2016-07-31T10:00:00.000+0000"), 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"account_identifier" : "5000", 
"account_name" : "Cost of sales - goods", 
"account_category" : "Sales Expense", 
"account_type" : null, 
"account_value_type" : "debit", 
"account_value" : NumberInt(10000) 
} 
{ 
"_id" : { 
    "party_uuid" : "phildominickcompany", 
    "connection_uuid" : "5738fc661a21db15b5c45b49", 
    "account_balances_date" : ISODate("2016-07-31T10:00:00.000+0000"), 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"account_identifier" : "5010", 
"account_name" : "Cost of sales - materials", 
"account_category" : "Sales Expense", 
"account_type" : null, 
"account_value_type" : "debit", 
"account_value" : NumberInt(20000) 
} 
{ 
"_id" : { 
    "party_uuid" : "phildominickcompany", 
    "connection_uuid" : "5738fc661a21db15b5c45b49", 
    "account_balances_date" : ISODate("2016-07-31T10:00:00.000+0000"), 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"account_identifier" : "6200", 
"account_name" : "Marketing", 
"account_category" : "Other Expense", 
"account_type" : null, 
"account_value_type" : "debit", 
"account_value" : NumberInt(1500) 
} 

私が今、適用しようとしている段階は次のとおりです。年と月で合計account_valueとなっています。私はEOOから日付に行くことを試みているというエラーを生成しています。

$group : { 
_id: {"party_uuid" : "$party_uuid", 
    "account_category" : "$account_category", 
    "account_balances_year" : {$year : "$account_balances_date"}, 
    "account_balances_month" : {$month : "$account_balances_date"}, 
    "account_category" : "$account_category", 
    "account_type" : "$account_type", 
    "object_origin_category" : "$object_origin_category", 
    "object_origin" : "$object_origin"}, 
    "month_value" : { $sum: "$account_value"} 
} 

おかげで、マット・

+0

こんにちはマットを、あなたはJSONを投稿することができます。 – rroxysam

答えて

1

は、サンプルから次$groupパイプライン

{ 
    "$group": { 
     "_id": { 
      "year": { "$year": "$_id.account_balances_date" }, 
      "month": { "$month": "$_id.account_balances_date" } 
     }, 
     "total_account_value": { "$sum": "$account_value" } 
    } 
} 

にこの意志の出力を追加します。

/* 1 */ 
{ 
    "_id" : { 
     "year" : 2016, 
     "month" : 7 
    }, 
    "total_account_value" : 89228.33 
} 

/* 2 */ 
{ 
    "_id" : { 
     "year" : 2016, 
     "month" : 6 
    }, 
    "total_account_value" : 0 
} 
+1

素晴らしい、これのおかげで、完全に動作します。 –

1

まず、あなたは年&月別でグループそして&年&月を取得するには、フィールドの$ account_balances_dateを投影する必要があります。以下のようなもの:

以前のグループByを使用している場合、このクエリを少し変更する必要があります。年と月でaccount_valueを合計する

関連する問題