How to convert MongoDB aggregation query to Laravel MongoDB by jenssegers How to convert MongoDB aggregation query to Laravel MongoDB by jenssegers mongodb mongodb

How to convert MongoDB aggregation query to Laravel MongoDB by jenssegers


You are both better off using the aggregation framework methods and also diving into the raw MongoDB collection object provided from the underlying driver to do so. It's a much better option that trying to translate the syntax:

// Returns the original Mongo Result$result = DB::collection('changes')->raw(function($collection){    return $collection->aggregate(array(        array(            '$group' => array(                '_id' => '$field',                'count' => array(                    '$sum' => 1                )            )        )       ));});

The result is a little different from the result of a method like .group() but this uses native code on the MongoDB server and does not rely on JavaScript interpretation like the .group() method actually does, being really a wrapper around mapReduce.

The end result is much faster, and also generally more efficient than you will get out of the native framework interface.

So use the native MongoDB way for the best performance.


Even if it's an question, there's an unseful entry in the repository wiki: Complex aggregate call

Hope it may help other people like me ;)