Mongo groupby is not working with different time in Laravel raw method
In the $group
you are using $checkin_from
directly as a date with time hence it is grouped by date AND time.
The answer is to use $dateFormat
so we have only the date without the time:
'_id' => ['checkin_from' => ['$dateFormat' => ['date' => '$checkin_from', format => '%Y-%m-%d']], 'cabinname' => '$cabinname']
You can use this group stag, Where I am convert string into number. total_prepayment_amount
and prepayment_amount
are string as per provided data they should be integer to $sum
.
[ '$group' =>[ '_id' => [ 'checkin_from' => '$checkin_from', 'cabinname' => '$cabinname' ], 'total_prepayment_amount' => [ '$sum' => ['$toInt' => '$total_prepayment_amount'] ], 'prepayment_amount' => [ '$sum' => ['$toInt' => '$prepayment_amount'] ], ], ],
like this in query for mongodb
total_prepayment_amount: {$sum : { $toInt: "$total_prepayment_amount"}},
Please make sure mongodb ^4.0 (for use $toInt)