How to use MongoDB PHP Library in test web app

Viewed 823

I have installed a new server running (alpine) linux with Apache and PHP7. I have also installed the mongodb driver. The following is what I see when I run phpinfo():

mongodb support enabled
mongodb version 1.1.10-dev
mongodb stability   devel
libmongoc version   1.3.6
libbson version 1.3.6

I have also installed the mongodb php7 library (https://pkgs.alpinelinux.org/package/edge/testing/x86_64/mongo-php7-library) and it created the following dir structure for me:

webdev1:/var/www/# ls -lah /usr/lib/php7/vendor/mongodb/mongodb/
total 104
drwxr-xr-x    6 root     root        4.0K Aug 16 18:03 .
drwxr-xr-x    3 root     root        4.0K Aug 15 18:07 ..
-rw-r--r--    1 root     root        5.1K Sep 23  2016 BulkWriteResult.php
-rw-r--r--    1 root     root        5.3K Aug 16 18:03 Client.php
-rw-r--r--    1 root     root       27.1K Sep 23  2016 Collection.php
-rw-r--r--    1 root     root        9.6K Sep 23  2016 Database.php
-rw-r--r--    1 root     root        1.3K Sep 23  2016 DeleteResult.php
drwxr-sr-x    2 root     root        4.0K Aug 15 18:07 Exception
-rw-r--r--    1 root     root        2.0K Sep 23  2016 InsertManyResult.php
-rw-r--r--    1 root     root        2.0K Sep 23  2016 InsertOneResult.php
drwxr-sr-x    2 root     root        4.0K Aug 15 18:07 Model
drwxr-sr-x    2 root     root        4.0K Aug 15 18:07 Operation
-rw-r--r--    1 root     root        3.3K Sep 23  2016 UpdateResult.php
-rw-r--r--    1 root     root        4.3K Sep 23  2016 functions.php
drwxr-sr-x    6 root     root        4.0K Aug 16 17:41 tests

I'm trying to write a basic page now that will prove that this library is working.
So far I have:

  1. Copied the library files into a sub folder in my test project. So i have this in my test web app folder:

    webdev1:/var/www/localhost/htdocs/test# ls -lah
    total 16
    drwxr-sr-x    3 root     root        4.0K Aug 16 18:41 .
    drwxr-sr-x    4 root     root        4.0K Aug 16 18:30 ..
    drwxr-xr-x    6 root     root        4.0K Aug 16 18:44 mongodb
    -rw-r--r--    1 root     root         516 Aug 16 18:39 test.php
    
    webdev1:/var/www/localhost/htdocs/test# ls -lah mongodb/
    total 104
    drwxr-xr-x    6 root     root        4.0K Aug 16 18:44 .
    drwxr-sr-x    3 root     root        4.0K Aug 16 18:41 ..
    -rw-r--r--    1 root     root        5.1K Aug 16 18:32 BulkWriteResult.php
    -rw-r--r--    1 root     root        5.3K Aug 16 18:32 Client.php
    -rw-r--r--    1 root     root       27.1K Aug 16 18:32 Collection.php
    -rw-r--r--    1 root     root        9.6K Aug 16 18:32 Database.php
    -rw-r--r--    1 root     root        1.3K Aug 16 18:32 DeleteResult.php
    drwxr-sr-x    2 root     root        4.0K Aug 16 18:32 Exception
    -rw-r--r--    1 root     root        2.0K Aug 16 18:32 InsertManyResult.php
    -rw-r--r--    1 root     root        2.0K Aug 16 18:32 InsertOneResult.php
    drwxr-sr-x    2 root     root        4.0K Aug 16 18:32 Model
    drwxr-sr-x    2 root     root        4.0K Aug 16 18:32 Operation
    -rw-r--r--    1 root     root        3.3K Aug 16 18:32 UpdateResult.php
    -rw-r--r--    1 root     root        4.3K Aug 16 18:32 functions.php
    drwxr-sr-x    6 root     root        4.0K Aug 16 18:41 tests
    
  2. Create a test.php page with the following code:

    "; echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n"; echo "Test 2 - MongoDB Client Library working...
    "; $client = new MongoDB\Client("mongodb://localhost:27017"); $collection = $client->jjtest->col1; $result = $collection->find(); var_dump($result); ?>

Results

When I run the test.php page, it shows that my driver is loaded... but I'm getting the error:

Test 1 - Mongodb Driver loaded?
loaded Test 2 - MongoDB Client Library working...

Fatal error: Uncaught Error: Class 'MongoDB\Database' not found in /var/www/localhost/htdocs/test/mongodb/Client.php:168 Stack trace: #0 /var/www/localhost/htdocs/test/mongodb/Client.php(93): MongoDB\Client->selectDatabase('jjtest') #1 /var/www/localhost/htdocs/test/test.php(9): MongoDB\Client->__get('jjtest') #2 {main} thrown in /var/www/localhost/htdocs/test/mongodb/Client.php on line 168

What I've tried So Far

To prove that mongo itself is working, I launched a CLI and ran this command:

> use jjtest
switched to db jjtest
> show collections
col1
> db.col1.find();
{ "_id" : ObjectId("5994980ba9e72bb4f6afed66"), "name" : "john", "age" : "99" }
>

I'm currently reading up on namespaces and the autoloader.php (Why my autoload.php of composer doesn't work?) but nothing I've tried so far is working there either. The files that were auto created by installing the mongo-php7-library doesn't include an autoloader.php that i can point to.

Update 1

I also tried to include this at the top of my test.php file:

 require_once __DIR__ . "/mongodb/";

But I get the following error:

Warning: require_once(/var/www/localhost/htdocs/test/mongodb/): failed to open stream: No error information in /var/www/localhost/htdocs/test/test.php on line 1

Fatal error: require_once(): Failed opening required '/var/www/localhost/htdocs/test/mongodb/' (include_path='.:') in /var/www/localhost/htdocs/test/test.php on line 1

1 Answers
Related