Skip to content

query the collection to improve performance #2589

Answered by GromNaN
nemesisKO asked this question in General
Discussion options

You must be logged in to vote

You cannot use Query\Builder::raw(Closure) with other methods of the query builder.

Try moving the where part into a $match stage.

$groupByResults = $result->raw(function ($collection) {
    return $collection->aggregate([
        [
            '$match' => $searchArray,
        ],
        [
            '$group' => [
                "_id" => ['$substr' => ['$hs_code', 0, 2]],
                "success_num" => ['$sum' => '$total_value_usd'],
            ],
        ],
        [
            '$sort' => [
                "success_num" => -1,
            ],
        ],
        [
            '$facet' => [
                "data" => [['$skip' => 0 * 10], ['$limit' => 10]],
            ],
        ],
 …

Replies: 1 comment

Comment options

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

This discussion was converted from issue #2540 on August 31, 2023 14:57.