-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Running raw aggregations #2079
-
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']],
]);
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🚀 1
Replies: 1 comment
-
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)
});
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🎉 1 -
❤️ 1 -
🚀 1