Laravel MongoDb match created_at gte/lte doesnt work

Viewed 22

I want to match documents gte/lte a date juste before grouping results but it doesn't work.

It's a basic example of e-commerce. I need to get sells in a date range and group by seller. I tried to use Carbon or UTCDateTime like below, but no effect... it returns empty :

$startDate = new \MongoDB\BSON\UTCDateTime(strtotime("2022-03-01") * 1000);

$startDate = \Carbon\Carbon::createFromFormat('Ymd', "20220301");

I'm using Laravel and MongoDb with Jenssegers. Here is my query :

$query = [
                [
                    '$unwind' => [
                        'path' => '$products',
                        'preserveNullAndEmptyArrays' => true,
                    ],
                ],
                [
                    '$match' => [
                        'products.product.subcategory' => 'parcours-golf',
                    ],
                ],
                [
                    '$match' => [
                        'created_at' => ['$gte', $startDate],
                    ],
                ],
                [
                    '$group' => [
                        '_id' => '$products.product.title',
                        'total' => ['$sum' => ['$multiply' => ['$products.price', '$products.quantity']]],
                        'quantity' => ['$sum' => '$products.quantity'],
                        'state' => ['$first' => '$products.product.location.state'],
                    ],
                ],

            ];

Does someone have an idea or a solution ? Obviously I can't use whereBetween('dates') after aggregate...

0 Answers
Related