如何解决,超出了$ group的内存限制,但不允许外部排序。传递allowDiskUse:true

时间:2019-04-22 12:26:35

标签: laravel mongodb aws-documentdb

我将laravel与AWS-DocumentDB结合使用,我试图使聚合器根据日期,国籍,性别和年龄对用户集合进行分组和汇总,所以我做了以下事情:

$aggregator = [
    [ '$project' => ['gender' => true] ],
    [ '$group' => [ '_id' => '$gender', 'type' => ['$first' => '$gender'], 'total' => ['$sum' => 1] ] ]
];

User::raw(function($collection) use ($aggregator) {
    return $collection->aggregate($aggregator, [ 'allowDiskUse' => true ]);
});

$aggregator = [
    [ '$project' => ['created_at' => true] ],
    [ 
        '$group' => [
            '_id'   => [ 'year' => ['$year' => '$created_at'], 'month' => ['$month' => '$created_at'] ],
            'total' => [ '$sum' => 1 ],
            'year'  => [ '$first' => ['$year' => '$created_at'] ],
        ],
    ],
    // ['$sort' => [ '_id.month' => 1 ]],
    [
        '$group' => [
            '_id'    => '$_id.year',
            'year'   => [ '$first' => '$year' ],
            'months' => [
                '$push' => [
                    'month' => '$_id.month',
                    'name'  => '$_id.month',
                    'total' => '$total'
                ]
            ]
        ]
    ],
];

return User::raw(function($collection) use ($aggregator) {
    return $collection->aggregate($aggregator, [ 'allowDiskUse' => true ]);
});

$aggregator = [
    [ '$project' => ['location' => true] ],
    [ '$group' => [ '_id' => '$location', 'name' => ['$first' => '$location'], 'code' => ['$first' => '$location'], 'total' => ['$sum' => 1] ] ]
];

return User::raw(function($collection) use ($aggregator) {
    return $collection->aggregate($aggregator, [ 'allowDiskUse' => true ]);
});

即使我传递了al​​lowDiskUse,它仍然给我这个错误$ group的内存限制已超出,但不允许外部排序。传递allowDiskUse:true

那么,如何解决这个问题呢?并且有什么方法可以将这个查询合并到一个查询中?

0 个答案:

没有答案