Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz
Created November 23, 2016 20:31
Show Gist options
  • Save bjpeterdelacruz/0c4dc6f143402f79616956158964d930 to your computer and use it in GitHub Desktop.
Save bjpeterdelacruz/0c4dc6f143402f79616956158964d930 to your computer and use it in GitHub Desktop.
Retrieving all documents from MongoDB database (PHP 5 vs. PHP 7)
// PHP 5
$m = new MongoClient();
$db = $m->test;
$collection = $db->todos;
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["name"] . "\n";
}
// PHP 7
$m = new MongoDB\Driver\Manager();
$query = new MongoDB\Driver\Query([]);
$collection = $m->executeQuery("test.todos", $query);
foreach($collection as $document) {
echo $document->name . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment