在Mongodb中如何给两个不同的$ match

时间:2018-07-26 06:18:19

标签: mongodb aggregation-framework match

在Db中,我有一些示例数据:

对象1

"_id" : ObjectId("5b5934bb49b")
"payment" : {
"paid_total" : 500,
    "name" : "havi",
    "payment_mode" : "cash",
    "pd_no" : "PD20725001",
    "invoices" : [ 
        {
            "invoice_number" : "IN11803831583"
        }
    ],
"type" : "Payment"
}

对象2

"_id" : ObjectId("5b5934ee31e"),
"patient" : {
    "invoice_date" : "2018-07-26",
    "invoiceTotal" : 2000,
    "pd_no" : "PD20725001",
    "type" : "Invoice",
    "invoice_number" : "IN11803831583"
}

注意:所有数据都在同一集合中

如上所示,我的数据库中有许多对象。如何从以上invoiceTotal的数据中求和与paid_total的总和,然后从paid_total中减去invoiceTotal并显示匹配{{1} }和pd_no

我期望的输出看起来像

invoice_number

2 个答案:

答案 0 :(得分:2)

样本输入:

{
    "_id" : ObjectId("5b596969a88e07f00d6dac17"),
    "payment" : {
        "paid_total" : 500,
        "name" : "havi",
        "payment_mode" : "cash",
        "pd_no" : "PD20725001",
        "invoices" : [
                {
                        "invoice_number" : "IN11803831583"
                }
        ],
        "type" : "Payment"
    }
}
{
    "_id" : ObjectId("5b596986a88e07f00d6dac18"),
    "patient" : {
        "invoice_date" : "2018-07-26",
        "invoiceTotal" : 2000,
        "pd_no" : "PD20725001",
        "type" : "Invoice",
        "invoice_number" : "IN11803831583"
    }
}

使用此汇总查询:

db.test.aggregate([
{
    $project : {
    _id : 0,
     pd_no : { $ifNull: ["$payment.pd_no", "$patient.pd_no" ] },
     invoice_no : { $ifNull: [ { $arrayElemAt : ["$payment.invoices.invoice_number", 0] },"$patient.invoice_number" ] },
     type : { $ifNull: [ "$payment.type", "$patient.type" ] },
     paid_total : { $ifNull: [ "$payment.paid_total", 0 ] },
     invoice_total : { $ifNull: [ "$patient.invoiceTotal", 0 ] },
    }
},
{
    $group : {
        _id : {
            pd_no : "$pd_no",
            invoice_no : "$invoice_no"
        },
        paid_total : {$sum : "$paid_total"},
        invoice_total : {$sum : "$invoice_total"}
    }
},
{
    $project : {
        _id : 0,
        pd_no : "$_id.pd_no",
        invoice_no : "$_id.invoice_no",
        invoice_total : "$invoice_total",
        paid_total : "$paid_total",
        balance : {$subtract : ["$invoice_total" , "$paid_total"]}
    }
}
])

在此查询中,我们首先找到pd_noinvoice_no,然后将它们用于对文档进行分组。接下来,我们得到invoice_totalpaid_total,然后减去它们得到balance

输出:

{
    "pd_no" : "PD20725001",
    "invoice_no" : "IN11803831583",
    "invoice_total" : 2000,
    "paid_total" : 500,
    "balance" : 1500
}

答案 1 :(得分:0)

我假设您将只拥有带有invoiceTotalpaid_total的文档,而不会同时拥有两个文档。

您首先需要获得一个金额来获取余额,因此,如果已支付的总额在发票总额的情况下需要为负数,则可以通过首先使用管道上的$ project来实现。

collection.aggregate([
{
    $project : {
        'patient.invoiceTotal': 1,
        'payment.paid_total': 1,
        ammount: { 
            $ifNull: ['$patient.invoiceTotal', { $multiply: [-1, '$payment.paid_total']}]
        }
    }
},
{
    $group: {
        _id: 'myGroup',
        invoiceTotal: { $sum: '$patient.invoiceTotal' },
        paid_total: { $sum: '$payment.paid_total' },
        balance: { $sum: '$ammount'  }
        }
}
])