简体   繁体   中英

Unrecognized option to $bucket: $group

getting the above error when i run $bucket with $group in mongodb laravel.

input collection:

{
    "id": "n54qllhzwdlxqvy",
    "season_id": "vl7oqdehzjxr510",
    "status_id": 8,
    "venue_id": "",
    "referee_id": "",
    "neutral": 1,
    "note": "",
    "home_scores": [0, 0, 0, 0, 0, 0, 0],
    "away_scores": [1, 0, 0, 0, 0, 0, 0],
    "home_position": "",
    "away_position": "",
    "coverage": {
        "mlive": 0,
        "lineup": 0
    },
    "round": {
        "stage_id": "ednm9whz7xoryox",
        "round_num": 0,
        "group_num": 3
    },
    "updated_at": "2021-05-03 16:53:52",
    "competition_id": "gpxwrxlhdgryk0j",
    "competition_name": "Chinese U18 Women's National Games",
    "home_team_id": "2y8m4zh8xwjql07",
    "home_team_name": "Fujian U18 Women ",
    "away_team_id": "8yomo4hvxg2q0j6",
    "away_team_name": "Shandong U18 Women",
    "match_time": "2021-05-03 15:00:00",
    "match_tsp": 1620025200,
    "created_at": "2021-05-04 14:33:05"
}
            $data = DB::connection('mongodb_football')->collection('match_list')->raw(function ($collection) {
        return $collection->aggregate([
            [
                '$bucket' => [ 
                    'groupBy' => '$competition_id',
                    'boundaries' => ["4zp5rzgh3nq82w1","4zp5rzghjjgq82w", "4zp5rzghkzq82w1","4zp5rzghpvnq82w","4zp5rzghx38q82w","4zp5rzghx5q82w1"],
                    'default' => "Other",
                    'output' => [
                        "data" => [
                            '$push' => [
                                "season_id" => '$season_id'
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    });

output:

need to group all data based on competition_id and need all the data.

  1. Remove $ prefix from groupBy , boundaries , default , and output .

The $bucket 'groupBy' field must be defined as a $-prefixed path or an expression

  1. It says group by field required reference field that should be prefix by $, but you have assigned it in array bracket [].

Try below syntax, i have not tested yet but it would be,

$data = DB::connection('mongodb_football')->collection('match_list')->raw(function ($collection) {
    return $collection->aggregate([
        [
            '$bucket' => [ 
                'groupBy' => '$competition_id',
                'boundaries' => ["4zp5rzgh3nq82w1","4zp5rzghjjgq82w", "4zp5rzghkzq82w1","4zp5rzghpvnq82w","4zp5rzghx38q82w","4zp5rzghx5q82w1"],
                'default' => "Other",
                'output' => [
                    "data" => [
                        '$push' => [
                            "season_id" => '$season_id'
                        ]
                    ]
                ]
            ]
        ]
    ]);
});

Playground

Solution

$data = DB::connection('mongodb_football')->collection('match_list')->raw(function ($collection) {
return $collection->aggregate([
    [
        '$bucket' => [ 
            'groupBy' => '$competition_id',
            'boundaries' => ["4zp5rzgh3nq82w1","4zp5rzghjjgq82w", "4zp5rzghkzq82w1","4zp5rzghpvnq82w","4zp5rzghx38q82w","4zp5rzghx5q82w1"],
            'default' => "Other",
            'output' => [
                "data" => [
                    '$push' => [
                        "season_id" => '$season_id'
                    ]
                ]
            ]
        ]
    ]
]);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM