根据平衡值数组计算运动值

时间:2019-02-07 21:04:11

标签: mongodb

有人知道如何计算文档之间的值差吗?因此,在当前文档值上减去先前的文档值以创建移动的新值。每个文档代表一个月和一年的余额。

我的文档中有一组account_balances,日期在每个月末。它们代表会计应用程序的总分类帐,其中集成仅提供余额,而不提供逐月变动。

如何从一个文件的数组和上个月的文件的数组中计算余额的差值?

要正确组合在一起的参数是_id.company,_id.connection,_id.object_snapshot_date,然后是account_balances.account_id和account_balances.value_type。

我想从2018-06-31文档的total_value中扣除2018-05-30中每个帐户的total_value。这里可能有多个与全年相关的文件。

我想要得到的是相同的凭证,但是6月份的total_value是变动而不是余额。

谢谢,马特

两个不同月份的文档的示例:

{
"_id" : {
  "company" : " a8aa7d3f-cef8-4895-a83e-3087b4cf529c ",
  "connection" : "a4b52d3a-0c00-406f-9163-4b1d52df0271",
  "object_snapshot_date" : 20180603135959,
  "object_schema" : "timeline-balance",
  "object_class" : "trial-balance",
  "object_category" : "balance",
  "object_type" : "month",
  "object_origin_category" : "bookkeeping",
  "object_origin_type" : "accounting",
  "object_origin" : "Xero"
 },
"account_balances" : [
{
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636c96f",
  "account_name" : "Sales",
  "account_code" : "200",
  "account_class" : "revenue",
  "account_category" : "sales",
  "account_group" : "",
  "value_type" : "credit",
  "total_value" : 29928.96,
  "value_currency" : "NZD"
},
{
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636aa43",
  "account_name" : "Cost of Goods Sold",
  "account_code" : "300",
  "account_class" : "expense",
  "account_category" : "sales",
  "account_group" : "",
  "value_type" : "debit",
  "total_value" : 12452.50,
  "value_currency" : "NZD"
}
]
},
{
"_id" : {
  "company" : " a8aa7d3f-cef8-4895-a83e-3087b4cf529c ",
  "connection" : "a4b52d3a-0c00-406f-9163-4b1d52df0271",
  "object_snapshot_date" : 20180503035959,
  "object_schema" : "timeline-balance",
  "object_class" : "trial-balance",
  "object_category" : "balance",
  "object_type" : "month",
  "object_origin_category" : "bookkeeping",
  "object_origin_type" : "accounting",
  "object_origin" : "Xero"
 },
"account_balances" : [
{
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636c96f",
  "account_name" : "Sales",
  "account_code" : "200",
  "account_class" : "revenue",
  "account_category" : "sales",
  "account_group" : "",
  "value_type" : "credit",
  "total_value" : 24231.12,
  "value_currency" : "NZD"
},
{
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636aa43",
  "account_name" : "Cost of Goods Sold",
  "account_code" : "300",
  "account_class" : "expense",
  "account_category" : "sales",
  "account_group" : "",
  "value_type" : "debit",
  "total_value" : 6875.10,
  "value_currency" : "NZD"
}
]
}

预期的输出将是这样的:

{
"_id" : {
  "company" : " a8aa7d3f-cef8-4895-a83e-3087b4cf529c ",
  "connection" : "a4b52d3a-0c00-406f-9163-4b1d52df0271",
  "object_snapshot_date" : 20180603135959,
  "object_schema" : "timeline-balance",
  "object_type" : "month",
  "object_origin_category" : "bookkeeping",
  "object_origin" : "Xero"
},
"account_movements" : [
 {
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636c96f",
  "account_name" : "Sales",
  "account_code" : "200",
  "account_class" : "revenue",
  "movement" : 5697.84
 },
 {
  "account_id" : "47cf9c6e-4ec7-4853-9efa-9e180636aa43",
  "account_name" : "Cost of Goods Sales",
  "account_code" : "200",
  "account_class" : "revenue",
  "movement" : 5577.4
 }
]
}

1 个答案:

答案 0 :(得分:1)

我假设您始终可以设置一个过滤条件,以确保在$match之后(如下面)仅保留两个文档。然后,您可以使用$unwind每个文档获取一个account_balance。在下一阶段,您可以$sortsnapshot_date。然后,您可以$groupaccount_name$push来获得全部balances。由于假设只有两个元素,因此您可以将$subtract$arrayElemAt一起使用来获得移动。

db.col.aggregate([
    {
        $match: {
            "_id.object_snapshot_date": {
                $gte: 20180500000000, 
                $lte: 20180630000000
            }
        }
    },
    {
        $unwind: "$account_balances"
    },
    {
        $sort: { "_id.object_snapshot_date": 1 }
    },
    {
        $group: {
            _id: "$account_balances.account_name",
            balances: { $push: "$account_balances.total_value" }
        }
    },
    {
        $project: {
            _id: 0,
            account_name: "$_id",
            movement: { $subtract: [ { $arrayElemAt: [ "$balances", 1 ] }, { $arrayElemAt: [ "$balances", 0 ] } ] }
        }
    }
])

输出:

{ "account_name" : "Cost of Goods Sold", "movement" : 5577.4 }
{ "account_name" : "Sales", "movement" : 5697.84 }

如果您需要更通用的解决方案(两个月以上),则可以用以下内容替换上一个管道阶段:

{
        $project: {
            _id: 0,
            account_name: "$_id",
            movement: {
                $map: {
                    input: { $range: [ 1, { $size: "$balances" } ] },
                    as: "index",
                    in: {
                        $subtract: [
                            { $arrayElemAt: [ "$balances", "$$index" ] },
                            { $arrayElemAt: [ "$balances", { $subtract: [ "$$index", 1 ] } ] }
                        ]
                    }
                }
            }
        }
    }

这将使用来计算balances数组中所有值的差(您将获得n-1个结果,其中n的大小为balances)。