如何使用mongodb扩展 [英] How to use mongodb extension

查看:104
本文介绍了如何使用mongodb扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码通过Php7连接到mongoDB

Hi I am trying to connect to mongoDB with Php7 using following code

$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");

但出现以下错误:

[:error] [pid 6069] [client 127.0.0.1:35118] PHP注意:未定义的属性:MongoDB \ Driver \ Manager :: $ clients

[:error] [pid 6069] [client 127.0.0.1:35118] PHP Notice: Undefined property: MongoDB\Driver\Manager::$clients

$clients是我要连接的mongodb数据库. MongoDB扩展已安装并包含在Php.ini中,如下所示:

Where $clients is the mongodb database I am trying to connect to. MongoDB extension is installed and included in Php.ini like this:

extension=mongodb.so

按如下所示连接到客户端数据库时

When connecting to clients database as follows

$db = $connection->clients;

我收到此错误:

试图获取非对象的属性"

"Trying to get property of non-object"

phpinfo()还将显示已安装的驱动程序,如以下屏幕截图所示

phpinfo() is also showing driver installed as shown in the following screenshot

我正在本地主机上运行apache2服务器.知道可能是什么问题吗?

I am running apache2 server on localhost. Any idea what could be the problem??

如果我执行var_dump($connection),则会得到以下提示:

If I do var_dump($connection) I get following:

object(MongoDB \ Driver \ Manager)#1(2){["uri"] =>字符串(25)"mongodb://localhost:27017" ["cluster"] => array(0){} }

object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(0) { } }

推荐答案

使用Manager api进行查询的正确方法如下.它无权访问db对象.

The right way to query using Manager api is below. It doesn't have access to the db object.

http://php.net/manual/zh/class .mongodb-driver-manager.php

这里是一个示例

$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$id           = new \MongoDB\BSON\ObjectId(SomeId);
$filter      = ['_id' => $id];

$query = new MongoDB\Driver\Query($filter);

$cursor = $connection->executeQuery('DatabaseNameHere.CollectionNameHere', $query);    

另一种查询方式是通过composer加载程序使用Client api.

Alternate way of query is to use Client api through composer loader.

安装:

https://docs.mongodb.com/php-library/master/tutorial/install-php-library/

教程

https://docs.mongodb.com/php-library/master/教程/

这篇关于如何使用mongodb扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆