How to connect php7 with mongoDB

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I'm trying to connect PHP 7 with mongoDB, I installed the "new" MongoDB driver using pecl by following this page instructions. I can see MongoDB version 1.1.8 from phpInfo() output, but I can't figure out how to initiate a connection from PHP code :p . the following code includes my attempts to connect (tried to connect even using old fashion way)

// new fashion way $connection = new MongoDB\Driver\Client();  // or by using old fashion way $conn = new MongoClient();  // random try :p $randConn = new MongoDB\Client(); 

and in both cases, I'm getting not defined class exception. please let me know what I'm missing and where is my mistake, please provide and example to be easier to follow if possible ;) .

PS: used operating system is ubuntu 14.04 LTS.


thanks in advance.

回答1:

The page that you are referring to is the low-level PHP driver for MongoDB. The API is the same as the HHVM driver for MongoDB. The documentation for both of them is the same, and can be found at http://docs.php.net/manual/en/set.mongodb.php

The driver is written to be a bare bone layer to talk to MongoDB, and therefore misses many convenience features. Instead, these convenience methods have been split out into a layer written in PHP, the MongoDB Library. Using this library should be your preferred way of interacting with MongoDB.

The library needs to be installed with Composer, a package manager for PHP. See also Get Composer: Installation on Linux/OSX

For example:

composer require "mongodb/mongodb=^1.0.0" 

Once you have it installed, you can try connecting using:

dbname->coll; ?> 

See also:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!