Reputation: 1186
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.
Upvotes: 6
Views: 17463
Reputation: 18835
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:
<?php
require 'vendor/autoload.php';
$collection = (new MongoDB\Client("mongodb://127.0.0.1:27017"))->dbname->coll;
?>
See also:
Upvotes: 9