Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose聚合查询的更好分组_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongoose聚合查询的更好分组

Javascript Mongoose聚合查询的更好分组,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,关于我收藏结构的一些背景信息。我有各种类型的金融交易,都使用discriminatorKey保存到一个集合中。以下是一个例子: "_id" : ObjectId("5816346ef201a84e17a84899"), "accountUpdatedBy" : -9.95, "transactionAmount" : 9.95, "paidTo" : "Vimeo", "expenseCategory" : "advertising", "account

关于我收藏结构的一些背景信息。我有各种类型的金融交易,都使用discriminatorKey保存到一个集合中。以下是一个例子:

 "_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
我需要做的是收集所有交易类型的所有总计,按年份和月份分组。在这里看了一些类似的问题之后,这是我提出的最好的问题:

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});
};
但是,这会产生如下结果:

 [ { 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' } ]
这方面的问题相当清楚。理想情况下,我希望找到一种稍微整合数据的方法,以减少格式化响应时的迭代。一些允许我嵌套的东西,以便最终得到如下结果:

    [{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和编程都是新手。

试试这个

`

`试试这个

`

`

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