2

当我在 mongodb laravel 中运行$bucket时出现上述错误。$group

输入集合:

{
    "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'
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    });

输出:

需要根据所有数据对所有数据进行分组,competition_id并且需要所有数据。

4

2 回答 2

0
  1. groupBy从、boundariesdefault和中删除 $ 前缀output

' groupBy $bucket' 字段必须定义为以 $ 为前缀的路径或表达式

  1. 它说按字段分组所需的参考字段应该以 $ 为前缀,但是您已将其分配在数组括号 [] 中。

试试下面的语法,我还没有测试过,但它会是,

$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'
                        ]
                    ]
                ]
            ]
        ]
    ]);
});

操场

于 2021-05-09T05:55:28.860 回答
0

解决方案

$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'
                    ]
                ]
            ]
        ]
    ]
]);

});

于 2021-05-09T07:15:50.483 回答