gpt4 book ai didi

php - 在 MongoDB PHP 驱动程序和相关 PHPLib 库中使用skip()和limit()实现简单分页时出错

转载 作者:行者123 更新时间:2023-11-29 18:10:15 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 从 MongoDB 访问数据时实现分页。在 MySQL 中,我会使用 OFFSETLIMIT。我的 Google 搜索告诉我,PHP-MongoDB 的替代方案是 skip()limit()

但是当我尝试使用这些函数时,出现了 fatal error 。这是错误:

Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Cursor::skip() in /var/www/html/Tests/test_mongo_two/test.php:12 Stack trace: #0 {main} thrown in /var/www/html/Tests/test_mongo_two/test.php on line 12

以下是演示该问题的示例 (SSCCE)。问题是我哪里出错了?我缺少什么?我该如何解决这个问题?

<?php 

require 'vendor/autoload.php';
$connection = new MongoDB\Client("mongodb://localhost:27017");


$db = $connection->Traffic;
$collection = $db->frameLengthsCollection;


#$allDataCursor = $collection->find();
$allDataCursor = $collection->find()->skip(25)->limit(25);
#$allDataCursor = $allDataCursor->limit(25);
#$allDataCursor = $allDataCursor->skip(25);





/**
* Prettifying (is that a word?) of data
*/
$allDataCursorConvertedToArray = array();
foreach ($allDataCursor as $key => $value) {
$json = MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($value));
$allDataCursorConvertedToArray[] = json_decode($json, true);
}




/**
* Display!
*/
#echo "allDataCursorConvertedToArray: "; print_r($allDataCursorConvertedToArray); echo "<br><br>";
//
foreach ($allDataCursorConvertedToArray as $key => $value) {
print_r($value);
break;
}

?>

最佳答案

尝试使用这个:

<?php 

$filter = [];
$options = [
'limit' => 25,
'skip' => 25
];

$allDataCursor = $collection->find($filter, options);

?>

关于php - 在 MongoDB PHP 驱动程序和相关 PHPLib 库中使用skip()和limit()实现简单分页时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com