Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

group by count mongodb laravel

<?php 

$category_ids = []; //categories goes here

$results = Post::raw(function($collection) use ($category_ids) {
    return $collection->aggregate([
        //where in...
        [
            '$match' => [
                'category_id' => ['$in' => $category_ids]
            ]       
        ],  
        [   
            '$group' => [
                '_id' => '$user_id',
                'count' => ['$sum' => 1]   
            ]   
        ],  
        [   
            '$sort' => ['count' => -1]   
        ],  
        [   
            '$limit' => 10
        ],  
    ]); 
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #group #count #mongodb #laravel
ADD COMMENT
Topic
Name
3+7 =