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 aggregate not working

Hi,

Any body help me. I am using mongdb aggregate simple query but not return any result.

$data = Transaction::aggregate( [ [ "\$match" => [ "shift_id" => new ObjectID($data['shift_id']) ], ], [ "\$group" => [ "_id" => [ "payment_method" => "\$paymentMethod" ], "total" => [ "\$sum" => "\$transactionAmount", ] ], ], ] );

But when i run this query mongo and it returns result.

db.transactions.aggregate([ { $match: { shift_id: ObjectId("593d0ed750692f04ca6f2e44") } }, { $group: { _id: {paymentMethod:"$paymentMethod"}, "total": {$sum:"$transactionAmount"} } } ])

Thank you

I would stick with single quotes around the mongo variables

Transaction::aggregate(
                [
                    [
                        '$match' => [
                            'shift_id' => $data['shift_id']
                        ],
                    ],

                    [
                        '$group' => [
                            'id' => ['paymentmethod' => '$paymentMethod'],
                            "total" => ['$sum' => '$transactionAmount',]
                        ],
                    ],
                ]
            );

Hi, I'm having the same problem. I don't know the problem why. I read someone's comment that it was because mongodb driver returned Cursor object, But I tried to loop and got no luck.

@ace05, did you found your solutions ?