Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Running raw aggregations #2079

Unanswered
canavci2016 asked this question in General
Discussion options

Hi,

how can I run a raw aggregations following

$collection->aggregate([
['$unwind' => ['path' => '$accommodation_types', 'preserveNullAndEmptyArrays' => true]],
['$match' => ['accommodation_types' => ['$exists' => true]]],
['$match' => ['_id' => new \MongoDB\BSON\ObjectId('5eb149b6e0b2555e66013e26')]],
['$group' => ['_id' => '$accommodation_types', 'count' => ['$sum' => 1]]],
['$addFields' => ['type' => '$_id']],
]);

You must be logged in to vote

Replies: 1 comment

Comment options

Hi,

I had the same question when I startet working with this package. So even if the question was asked long time ago, maybe the answer is helpfull for other beginners.

You can use the 'raw()' function like this giving a closure as argument which returns your aggregation:

$results = $this->model_name::raw(function ($collection) {
return $collection->aggregate(['some complex query ...'])
});

You can also pass your complex query to the closure:

$query = ['some complex query ...'];

$results = $this->model_name::raw(function ($collection) use ($query) {
return $collection->aggregate($query)
});

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #2079 on September 02, 2020 05:31.

AltStyle によって変換されたページ (->オリジナル) /