在更新之前,我的代码是:
// 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);
你能帮助我吗?谢谢!
解决方法:
您使用的Manager 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。