我正在使用mongodb驱动程序通过PHP连接到mongodb . 我无法使用mongoClient()连接到mongodb .
我的php和mongodb的版本
php --version输出
mongo --version输出
所以,我试图通过mongodb驱动程序连接它 . 这是代码......
$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(["number"=>$Number, "created"=>time()]);
$connection-> executeBulkWrite("dbName.collectionName", $bulk);
这可以很好地将数据插入mongodb .
现在,我想得到不 . 数据库中的集合中的行但无法找到任何解决方案 .
我尝试使用此代码从mongodb中检索数据:
try {
$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = ["number"=> $Number];
$query_mongo = new MongoDB\Driver\Query($filter);
$mongoResult = $connection->executeQuery("dbName.collectionName", $query_mongo);
$result = current($mongoResult->toArray());
var_dump($result);
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
echo "In file:", $e->getFile(), "\n";
echo "On line:", $e->getLine(), "\n";
}
但我在浏览器控制台中收到此错误...
parsererror,SyntaxError: Unexpected token o in JSON at position 0,object(stdClass)#26 (3) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#25 (1) {
["oid"]=>
string(24) "587c6e37e2d11616411c90a2"
}
["number"]=>
string(10) "123456"
["created"]=>
int(1484549687)
}
Q1 . 我可以摆脱这个Mongodb \ Driver \ Manager并使用旧的mongoClient和如何?
Q2 . 我想要没有 . 表中的行数 . 我怎么能用这个Driver \ Manager做到这一点?
Q3 . 对给定错误的任何解决方案?
提前致谢...