We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Mongodb Aggregation always returns empty

Hi everyone, I'm having problem with mongodb aggreagation. I don't know why but the command always return empty result even with the simplest command ($match only 1 property). Here is the example code :

$data = Orders::aggregate([
                [
                    '$match' => [
                        'userId' => $this->user->getId(),
                        'domain' => $domain,
                        'created_at' => [
                            '$gte' => new UTCDateTime(1000 * mktime(0, 0, 0, date("n", $from), date('j', $from), date('Y', $from))),
                            '$lte' => new UTCDateTime(1000 * mktime(0, 0, 0, date("n", $to), date('j', $to), date('Y', $to)))
                        ]
                    ],
                ],
                [
                    '$unwind' => '$fulfillments_data'
                ],
                [
                    '$group' => [
                        '_id' => [
                            'month' => [
                                '$month' => '$created_at'
                            ],
                            'day' => [
                                '$dayOfMonth' => '$created_at'
                            ],
                            'year' => [
                                '$year' => '$created_at'
                            ]
                        ],
                        'revenue' => [
                            '$sum' => '$total_price'
                        ],
                        'shopify_fee' => [
                            '$sum' => '$shopify_transaction_fee'
                        ],
                        'paygate_fee' => [
                            '$sum' => '$paygate_transaction_fee'
                        ],
                        'fulfilment_fee' => [
                            '$sum' => '$fulfillments_data.orderPrice'
                        ],
                    ]
                ]
            ]);

Any Suggestion ?

Thanks

edited Jun '18

I had head scratchers like this and it came down to the date formatting. you should use the \MongoDB\BSON\UTCDateTime format your date first then convert it to BSON\UTCDateTime $bsDate = new \MongoDB\BSON\UTCDateTime(new DateTime($yourdatetimestring));