2017-06-29 5 views
1

Ich habe ubuntu 16.04, php7 und mongo.Probleme mit php7 mongo Abfrage findOne

Nach der Aktualisierung des Systems funktioniert mein Code nicht ... Ich habe eine neue Version von PHP.

Vor Update, mein Code war:

// connect 
    $m = new MongoClient(); 
    // select a database 
    $db = $m->clients; 
    // select a collection (analogous to a relational database's table) 
    $collection = $db->suscriptions; 
    // Check if exists in DB 
    $query = array('email' => $email); 
    $cursor = $collection->findOne($query); 

Nach der Aktualisierung ich die Verbindung geändert, wie durch die PHP-Dokumentation angegeben, aber ich kann jede Abfrage nicht tun ... Dies ist mein Code, wenn Ich entferne die letzte Zeile, der Code funktioniert:

// connect 
    $m = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 
    // select a database 
    $db = $m->clients; 
    // select a collection (analogous to a relational database's table) 
    $collection = $db->suscriptions; 
    // Check if exists in DB 
    $query = array('email' => $email); 
    // Problem 
    $cursor = $collection->findOne($query); 

Können Sie mir helfen? Vielen Dank!

Antwort

4

Sie Verwendung von Manager API ist falsch.

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 
$filter= array('email' => $email); 
$options = array(
    'limit' => 1 
); 
$query = new MongoDB\Driver\Query($filter, $options); 
$rows = $m->executeQuery('clients.suscriptions', $query); 

Alternativ sollten Sie die Bibliothek durch composer installieren, die ähnliche Syntax wie alt api bietet.

require 'vendor/autoload.php'; 
$m= new MongoDB\Client("mongodb://127.0.0.1/"); 
$db = $m->clients; 
$collection = $db->suscriptions; 
$query = array('email' => $email); 
$document = $collection->findOne($query); 

https://docs.mongodb.com/php-library/master/tutorial/crud/#find-one-document

+0

Thank you !!! Funktioniert perfekt! – Miguel