MongoDB query for aggregate function retuning empt

2019-07-20 12:08发布

Following is the query i had written to get the count based on event name.

$data= DB::collection(Events::$collection)->raw(function($collection) {
    return $collection->aggregate([
    ['$match' => ['appid' => 1]],
    ['$group' => ['_id' =>['Event' =>'$Event'], 'count' =>['$sum'=>1]]]
    ]);
});

When i print the above query using laravel eloquent query logs its displays the output as follows :

Array ( 
[query] => events.aggregate([{"$match":{"appid":1}},{"$group":{"_id":{"Event":"$Event"},"count":{"$sum":1}}}]) 
[bindings] => Array ( ) [time] => 9.93 
)

DB Query As Follows:

db.events.aggregate([{"$match":{"appid":1}},{"$group":{"_id":{"Event":"$Event"},"count":{"$sum":1}}}])

The same thing if i run in NoSQLBooster for MongoDB then its retuning the result properly.

But the laravel mongodb query from the above is returning empty result. I am not getting what is the problem. Needs help.

I am using following version of laravel and mongodb.

Laravel Version : 5.4
MongoDB Version : 3.6

Thanks in Advance.

0条回答
登录 后发表回答