João Paulo
João Paulo

Reputation: 6670

Fetch more than 100 records on MongoDB with PHP

I'm using the new MongoDB PHP driver. I've been searching how I can get more than 100 records from a query.

I'm using executeCommand to pass the query. I think that in the old driver, the cursor object had a getNext method to get other pages, but It does not have anymore. How can I get the other "pages" from my query?

Upvotes: 1

Views: 613

Answers (2)

dikesh
dikesh

Reputation: 3125

I tried this solution and it returns all the documents in collection.

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command(array('find' => "testColl"));
$cursor = $manager->executeCommand('testDb', $command);

print_r($cursor->toArray());
?>

Upvotes: 0

Alex Goncharenko
Alex Goncharenko

Reputation: 1066

Simply use MongoDB\Driver\Query and MongoDB\Driver\Manager::executeQuery methods.

Here's a short sample for demonstration:

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$query   = new MongoDB\Driver\Query([], []);
$cursor  = $manager->executeQuery('DB.Collection', $query);
$array   = $cursor->toArray();

Note that resulting $array contains documents (records) as instances of stdClass Object.

Upvotes: 1

Related Questions