I have a mongo collection in which i need to group the data by payment_id,further i need to get the total_payable
amount. The total_payable amount
is made of total_payable+extra_book_price
. Normal Addition is working but i want to target a scenario in which the extra_book_price
that is inside a detail array is empty. So far i have tried $exists
and ifNull
but they don't work .
Any help is appreciated.
Also if extra_book_price
does not exist i need to return the total_payable
only.
[
'$match'=>[
'bookfair_id'=>110
]
],
[
'$group'=> [
'_id'=>'$payment_id',
'card_pay'=> ['$sum'=> '$total_payable' ],
'count'=>[
'$sum'=>[
'$cond'=> [
'if'=> ['$ifNull'=>['$extra_books_detail',true]],
'then'=>['$total_payable'],
'else'=>['$add'=> ['$total_payable','$extra_books_detail.total_payable'] ]
]
]
]
]
],
DUMMY DATA
{
"_id":23761,
"gross_total":2499,
"total_payable":2499,
"bookfair_id":110,
"payment_id":2,
"order_detail":[
{
"product_id":"5c63d418ae1db123d8048276",
"amount":2499,
"quantity":1,
"status":1
}
],
"extra_books_detail":{
"employee_id":0,
"total_payable":"100",
"payment_id":1,
"detail":[
{
"amount":"100",
"quantity":1
}
]
},
"status":1
}
AND IF EXTRA BOOK DETAIL
is empty
{
"_id":1,
"gross_total":350,
"total_payable":350,
"bookfair_id":300,
"payment_id":1,
"order_detail":[
{
"product_id":"5c4f2a5725956b7e8e7ddba8",
"condition_id":"5c5562df25956b10c02d06d3",
"amount":350,
"quantity":1,
"status":1
}
],
"extra_books_detail":[
],
"status":1,
"date_added":"2018-01-06T06:14:04.000Z"
}
</div>