0

I am using Laravel 5.6 with MongoDB using Jenssegers Package

Here I want to get total count using group by a method as we do in Mysql eg.

select latitude,longitude,count(*) as `count` from event_logger group by latitude,longitude

According to docs of the package, in MongoDB we can't use aggregate functions simple as we do in eloquent but instead we have to use raw query with MongoDB syntax.

public static function getUsersByTypeLogin()
{

        $q = self::raw()->aggregate(
            [
                array(
                    '$group'=>array(
                        '_id'=>array(
                            'lat'=>'$latitude',
                            'long'=>'$longitude'
                        ),
                        'count'=>array('$sum'=>1)
                    )
                )
            ]
        );

        dd($q);
}

When I do dd(dump and die), I am getting Cursor {#586} as result. So basically how to get/access data in cursor into my laravel application?

2 Answers 2

2

I'm using too this package a project. I'm using raw closure method that returns aggregation data.

For Example:

$data = YourModel::raw(function($collection)
{
    return $collection->aggregate(
        [
            array(
                '$group'=>array(
                    '_id'=>array(
                        'lat'=>'$latitude',
                        'long'=>'$longitude'
                    ),
                    'count'=>array('$sum'=>1)
                )
            )
        ]
    );
});

$data should be your data.

Can you try above codes?.

Sign up to request clarification or add additional context in comments.

Comments

0

Use

foreach ($q as $row) {
    //$row contains data for every row
 }

where q is the cursor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.