问题描述
我有Ubuntu 16.04,php7和mongo.
I've ubuntu 16.04, php7 and mongo.
更新系统后,我的代码不起作用...我有一个新版本的php.
After update the system, my code doesn't work... I've a new version of php.
在更新之前,我的代码是:
Before update, my code was:
// 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文档的指示更改了连接,但是我无法进行任何查询...这是我的代码,如果我删除最后一行,该代码将起作用:
After update, I changed the connection as indicated by the php documentation, but I can't do any query...This is my code, if I remove the last line, the code works:
// 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);
你能帮我吗?谢谢!
推荐答案
您使用的管理器api不正确.
You usage of manager api is incorrect.
$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类似的语法.
Alternatively, you should install the library through composer which provides similar syntax as old 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
这篇关于php7 mongo查询findOne的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!