2017-06-29 75 views
1

我有Ubuntu 16.04,php7和mongo。與php7 mongo查詢問題findOne

更新系統後,我的代碼不工作...我有一個新版本的PHP。

更新之前,我的代碼是:

// 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); 

更新後,我改變了連接由PHP文件指示,但我不能做任何查詢... 這是我的代碼,如果我刪除最後一行,代碼工作:

// 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); 

你能幫我嗎?謝謝!

回答

4

您對管理API的使用不正確。

$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); 

另外,您應該通過composer提供類似的語法舊的API安裝該庫。

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

謝謝!完美的作品! – Miguel