MongoDB PHP 限制无效

MongoDB PHP Limit not working

我在 MongoDB PHP 驱动程序中使用这段代码来获取数据库中的所有文档

$result = $collection->find();

foreach ($result as $doc) {
    print_r($doc);
}

但是,当为其添加限制时,它不再起作用:不再打印任何文档:

$result = $collection->find()->limit(10);

foreach ($result as $doc) {
    print_r($doc);
}

数据库中肯定有足够的文档。我不知道这是什么问题。

作为您上述问题的解决方案,请尝试执行以下代码片段。

$result = $collection->find();
$result->limit(10);
foreach ($result as $doc) {
    print_r($doc);
}

我已经通过查看测试版的源代码解决了这个问题。该文档似乎仅适用于旧版 mongo 扩展,而不适用于较新的 mongodb 扩展。

错误日志显示:Call to undefined method MongoDB\Driver\Cursor::addOption()。我查看了文档并得出结论 should 函数有效,因为它说 (PECL mongo >=0.9.0)。注意 mongo.

后缺少的 db

我通过以下方式修复了它: $collection->find([], [ 'limit' => 2 ]);,提供一个空的过滤器数组,然后在另一个数组中添加我的选项。

我遇到了同样的问题。有很多使用 $result = $collection->find()->limit(10);

的代码示例

事实证明,虽然这对 MongoDB PHP 驱动程序的原始版本完全有效,但同样的驱动程序有一个新版本。原驱动现在算"The Legacy Driver".

这是一个例子,“old”驱动应该如何使用:

<?php
$m = new MongoClient;

// Select 'demo' database and 'example' collection
$collection = $m->demo->example;

// Create the cursor
$cursor = $collection->find();

此时,虽然创建了游标对象,但查询尚未执行(即未发送到服务器)。查询只能通过使用 foreach ( $cursor as $result ) 或调用 $cursor->rewind() 开始迭代来执行。这使您有机会在服务器执行之前使用 sort()limit()skip() 配置游标的查询:

// Add sort, and limit
$cursor->sort( [ 'name' => 1 ] )->limit( 40 )

new驱动中,只要你有一个\MongoDB\Driver\Cursor对象,它就已经被服务器处理了.因为排序(以及限制和跳过)参数需要在执行查询之前发送到服务器,所以您不能在现有的 Cursor 对象上追溯调用它们。

这就是为什么不再像以前那样使用 limit() 方法的原因。此外,接受的答案是正确的。我想举一个更详细的例子:

$filter = [
                'author' => 'rambo',
                'views' => [
                    '$gte' => 100,
                ],
            ];
            $options = [
                /* Return the documents in descending order of searchPage */
                'sort' => [
                    'searchPage' => -1
                ],
                /* Limit to 2 */
                'limit' => 2,
                /* close the cursor after the first batch */
                'singleBatch' => true,
 ];

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

我正在尝试用新 php mongodb 驱动程序的示例进行描述。在示例中显示 skip,limit,Fields slection

require 'vendor/autoload.php'; // include Composer's autoloader

$client = new MongoDB\Client("mongodb://localhost:27017");



// SELECT * FROM YOUR_TABLE_NAME ;
// db.YOUR_COLLECTION_NAME.find({});
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array());

//SELECT * from YOUR_TABLE_NAME WHERE YOUR_COLUMN = "A"
// db.YOUR_COLLECTION_NAME.find({{ YOUR_FIELD: "A" }});
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('YOUR_FIELD'=>'A'));


//Return the Specified Fields and the _id Field Only

//SELECT _id, item,status YOUR_TABLE_NAME from inventory WHERE status = "A"
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1 } )

$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE)));

//Suppress _id Field
//SELECT item, status from YOUR_TABLE_NAME WHERE status = "A"
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE,'_id'=>FALSE)));

//SELECT * FROM YOUR_TABLE_NAME LIMIT 10
//db.YOUR_COLLECTION_NAME.find({}).limit(10);
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array(),array('limit'=>10));

//SELECT * FROM YOUR_TABLE_NAME LIMIT 5,10
//db.YOUR_COLLECTION_NAME.find({}).skip(5).limit(10)
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array(),array('skip'=>5,'limit'=>10));

//Suppress _id Field
//SELECT item, status from YOUR_TABLE_NAME WHERE status = "A" LIMIT 5,10;
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1, _id: 0 } ).skip(5).limit(10);
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE,'_id'=>FALSE),'skip'=>5,'limit'=>10));

foreach ($result as $entry){
    echo "<pre>";
    print_r($entry);
    echo "</pre>";
}