错误信息:
Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.
错误原因:
MongoDB:排序超出了内存限制的104857600字节
laravel操作mongodb分页遇到的问题
"msg": "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
实际上是mongodb排序超出了 104857600 字节的内存限制
需要在查询中加上allowDiskUse = true
mongodb语句如下: 查询时候需要加上 allowDiskUse = true
db.bigdata.aggregate(
[
{$group : {_id : "$range", total : { $sum : 1 }}},
{$sort : {total : -1}}
],
{allowDiskUse: true}
);
那么在laravel中怎么体现的呢?
模型
namespace App\Models\Backend;
use Jenssegers\Mongodb\Eloquent\Model;
class SensitiveWordMongodb extends Model
{
protected $connection = 'content_center';
protected $table = 'sensitive_word';
/***
* 白名单
* @var string[]
*/
protected $fillable = ['wordid', 'word', 'create_time', 'update_time'];
/**
* @var string
*/
protected $primaryKey = '_id'; //设置id
/**
* 指定是否模型应该被戳记时间。
*
* @var bool
*/
public $timestamps = false;
}
方法
public function getSensitiveWordList(array $params, int $page = 1, int $pagesize = 10): array
{
$page = $page <= 0 ? 1 : $page;
//限制一页获取最大条数
if ($pagesize <= 0 or $pagesize > env('MAX_PAGESIZE')) {
$pagesize = env('MAX_PAGESIZE');
}
$sensitiveWordMongodb = new SensitiveWordMongodb();
if (!empty($params['startTime']) && !empty($params['endTime'])) {
$sensitiveWordMongodb = $sensitiveWordMongodb
->where('create_time', '>=', strtotime($params['startTime']))
->where('create_time', '<', strtotime($params['endTime']) + 86400);
}
if (!empty($params['word'])) $sensitiveWordMongodb = $sensitiveWordMongodb->where('word', 'like', "%{$params['word']}%");
if (!empty($params['wordid'])) $sensitiveWordMongodb = $sensitiveWordMongodb->where('wordid', (int)$params['wordid']);
//计算总页数
$total = $sensitiveWordMongodb->count();
$totalPage = ceil($total / $pagesize);
$list = $sensitiveWordMongodb
->select(['wordid', 'word', 'create_time', 'update_time'])
->orderBy('create_time', 'desc')
->forPage($page, $pagesize)
->options(['allowDiskUse' => true]) // 解决mongodb排序超出了内存限制的问题
->get()
->toArray();
foreach ($list as &$v) {
$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
$v['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
}
return [
'page' => $page,
'total' => $total,
'totalPage' => $totalPage,
'list' => $list,
];
}
可见,在代码中加入 options() 方法即可
|