How to connect php7 to mongoDB

I am trying to connect PHP 7 to mongoDB, I installed the β€œnew” MongoDB driver using pecl by doing the following pages . I can see MongoDB version 1.1.8 version from phpInfo() , but I cannot figure out how to initiate a connection from PHP code: p. The following code includes my connection attempts (tried to connect even using the old method)

 // 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 get an exception class exception. please let me know what I am missing and where is my mistake, please provide an example to make it easier to follow if possible;).

PS: the operating system used is ubuntu 14.04 LTS.


early.

+5
source share
1 answer

The page you link to is a 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 as a bare bone layer to talk to MongoDB, and therefore misses many convenient features. Instead, these convenient methods were split into a layer written in PHP, the MongoDB Library . Using this library should be your preferred way to interact with MongoDB.

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

For instance:

 composer require "mongodb/mongodb=^1.0.0" 

After you installed it, you can try to connect using:

 <?php require 'vendor/autoload.php'; $collection = (new MongoDB\Client("mongodb://127.0.0.1:27017"))->dbname->coll; ?> 

See also:

+7
source

All Articles