How to setup PHP App With MongoDB

24 de octubre, 2017 @ 13:37 — 0 comentariosDownload PDF

This tutorial has been created having in mind, it's going to be used with services, but may be also valid for general purpose.

1.- Include MongoDB extension to your PHP setup

Here in the MongoDB extension is available starting on PHP 5.6. To enable the extension you need to access PHP Configuration:

How to modify PHP configuration

The new parameter in the configuration, should be as follow:

New parameter: extension =

Remember to push Save button.


Once this has been done, you can now check the extension is activated throught the phpinfo() function on a test file, it should be something like this:

2.- Use the Oficial MongoDB PHP Library

You can adquire this library, using composer command, wich is availabe on all our servers at The oficial documentation can be seen in this websites:


Remember you need to access to your hosting server using SSH service:

How to access your hosting via SSH


Once loged in, you may do something like this:

cd public_html
/opt/php7.0/bin/php /opt/cpanel/composer/bin/composer require mongodb/mongodb


3.- MongoDB PHP example


require 'vendor/autoload.php' ;


$client=new MongoDB\Client($uri);

$collection = $client->base_de_datos->productos;

$result = $collection->insertOne( [ 'item' => 'producto1', 'cantidad' => '200' ] );

echo "Inserted with Object ID '{$result->getInsertedId()}'";


Check the oficial documentation for further information: