MongoDB:使用嵌套值按年/月获取总和

mwg9r5ms  于 2022-12-29  发布在  Go
关注(0)|答案(2)|浏览(143)

我试图用嵌套的金额对一个集合求和(按月/年计算支出)-没有运气。
这是收集(摘录):

[
{
    "_id" : ObjectId("5faaf88d0657287993e541a5"),
    "segment" : {
        "l1" : "Segment A",
        "l2" : "001"
    },
    "invoiceNo" : "2020.10283940",
    "invoicePos" : 3,
    "date" : ISODate("2019-09-06T00:00:00.000Z"),
    "amount" : {
        "document" : {
            "amount" : NumberDecimal("125.000000000000"),
            "currCode" : "USD"
        },
        "local" : {
            "amount" : NumberDecimal("123.800000000000"),
            "currCode" : "CHF"
        },
        "global" : {
            "amount" : NumberDecimal("123.800000000000"),
            "currCode" : "CHF"
        }
    }
},
...
]

我想用“全球”货币总结每月的总发票量。
我在MongoDB上尝试了这个查询:

db.invoices.aggregate( 
       {$project : { 
              month : {$month : "$date"}, 
              year : {$year :  "$date"},
              amount : 1
          }},
        {$unwind: '$amount'}, 
        {$group : { 
                _id : {month : "$month" ,year : "$year" },  
              total : {$sum : "$amount.global.amount"} 
        }})

我得到的结果是:

/* 1 */
{
    "_id" : ObjectId("5faaf88d0657287993e541a5"),
    "amount" : {
        "document" : {
            "amount" : NumberDecimal("125.000000000000"),
            "currCode" : "USD"
        },
        "local" : {
            "amount" : NumberDecimal("123.800000000000"),
            "currCode" : "CHF"
        },
        "global" : {
            "amount" : NumberDecimal("123.800000000000"),
            "currCode" : "CHF"
        }
    },
    "month" : 9,
    "year" : 2019
}

/* 2 */
{
    "_id" : ObjectId("5faaf88d0657287993e541ac"),
    "amount" : {
        "document" : {
            "amount" : NumberDecimal("105.560000000000"),
            "currCode" : "CHF"
        },
        "local" : {
            "amount" : NumberDecimal("105.560000000000"),
            "currCode" : "CHF"
        },
        "global" : {
            "amount" : NumberDecimal("105.560000000000"),
            "currCode" : "CHF"
        }
    },
    "month" : 11,
    "year" : 2020
}

但是,这并不是每月所有发票的总和,而是看起来像单个发票行-没有汇总。
我想得到这样一个结果:

[
  {
    "month": 11,
    "year": 2020,
    "amount" : NumberDecimal("99999.99") 
  },
  {
    "month": 10,
    "year": 2020,
    "amount" : NumberDecimal("99999.99") 
  },
  {
    "month": 9,
    "year": 2020,
    "amount" : NumberDecimal("99999.99") 
  }
]

我的查询有什么问题?

cwdobuhd

cwdobuhd1#

这会有帮助吗?

db.invoices.aggregate([
  {
    $group: {
      _id: {
        month: {
          $month: "$date"
        },
        year: {
          $year: "$date"
        }
      },
      total: {
        $sum: "$amount.global.amount"
      }
    }
  },
  {$sort:{"_id.year":-1, "_id.month":-1}}
])

Playground
如果你需要任何额外的解释,让我知道,但代码是相当短和不言自明的。

rjee0c15

rjee0c152#

原则上你的聚合管道是好的,有几个错误:

  • 聚合管道需要数组
  • $unwind是无用的,因为$amount不是数组。
  • 可以直接使用日期函数

简单明了:

db.invoices.aggregate([
  {
    $group: {
      _id: { month: { $month: "$date" }, year: { $year: "$date" } },
      total: { $sum: "$amount.global.amount" }
    }
  }
])

相关问题