Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect php7 with mongoDB

Tags:

php

mongodb

php-7

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.

like image 322
rramiii Avatar asked Jul 11 '16 12:07

rramiii


People also ask

Can I connect PHP with MongoDB?

You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.

How enable MongoDB in PHP INI?

Install manually After executing the previous commands, you have to make a change in your php. ini file. Open the file in your favorite text editor and extension_dir variable is pointing to the location of mongo.so. Also, add 'extension=mongo.so' in a file, save and restart your web server.

What is a MongoDB driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.

How do I check my MongoDB driver version?

To check mongodb version use the mongod command with --version option. On windows you will have to use full path to the mongod.exe and mongo.exe to check mongodb version, if you have not set MongoDB Path. But if MongoDb Path is being set, you can simply use the mongod and mongo command.


1 Answers

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:

  • Doc: MongoDB PHP Library
  • MongoDB PHP Library: Getting Started
  • PHP MongoDB Driver
like image 91
Wan Bachtiar Avatar answered Sep 28 '22 07:09

Wan Bachtiar