MongoDB query for aggregate function retuning empty result

无人久伴 提交于 2019-12-11 02:34:58

问题


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.

来源:https://stackoverflow.com/questions/50796783/mongodb-query-for-aggregate-function-retuning-empty-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!