javascript - Better Grouping for Mongoose Aggregate Queries -


a bit of background information on structure of collection. i've got various types of financial transactions, saved 1 collection using discriminatorkey. here example:

 "_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 

what need gather of totals transaction types, grouped year , month. after taking @ similar questions here, best query i've come with:

transaction.findtransactioncategorytotalsbymonth = function () { return     .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}); }; 

however, produces results this:

 [ { 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' } ] 

the issues pretty clear. ideally, i'd find way consolidate data bit, reduce iteration when formatting response. allow me nest in order end result looks like:

    [{year: 2016,      sometransactiontype: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100], // amounts each month in array     othertransactiontype: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]}] 

or other way of getting number of objects down, more nesting.

very grateful advice. , apologize ahead of time if basic, i'm new javascript , programming in general.

you can try this. first grouping sum of each months total, followed sort, , second grouping push of month totals each category , final grouping total each year.

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"       }   }]) 

sample output

{     "categorytotal": [{         "transactioncategory": "donating",         "yeartotal": [15.95]     }, {         "transactioncategory": "campaigning",         "yeartotal": [10.95]     }, {         "transactioncategory": "advertising",         "yeartotal": [12.95, 21.9]     }],     "year": 2016 } 

Comments

Popular posts from this blog

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - SSE Emitter : Manage timeouts and complete() -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -