Getting started with the new php 8 world

Hi,
I installed everything from mongo db on ubuntu.
Everything’s seems to work by I don’t find a good tutorial to start.

My first lines are:

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
var_dump($manager);
$servers = $manager->getServers();
var_dump($servers);´

Ok, because of lazy binding I receive a blank array, got it!

But what do you ?

  1. Is the Mongo-db lib a server cluster or a client ?
  2. How to create a server ?
  3. How to create a DB on that server ?
  4. How to write to this DB easily i.e. with .Insert ( json ), not with low level ExecuteCommand…

Where to find a tutorial without MongoDB\Client ?? This does not help anymore!

Categories: Could not add “php 8.x” or similar.

Best and thanks a lot in advance
Robert

Hi Robert,

our Drivers documentation would be a good place to start and contains a few tutorials on how to do things in PHP. Note that there are no specifics for installing on PHP 8.x, as the driver supports PHP 7.2-8.2 at this time.

To clear up some confusion around the Manager and Client classes, our driver consists of two parts: the PHP extension (often referred to as PHPC, or ext-mongodb), which is a thin PECL extension to provide basic functionality. This is done to reduce the complexity of the PHP driver, as we’re able to leverage libmongoc to take care of things like server discovery, authentication, monitoring, etc.
The second part is the PHP library (PHPLIB, mongodb/mongodb on packagist), which provides the common drivers API that other drivers provide. In 99.9% of all cases, you’ll want to interact with that library, as that provides you with a high-level API. Once the library is installed, connecting to MongoDB and inserting data becomes a lot easier:

$client = new MongoDB\Client('mongodb://localhost:27017');
$collection = $client->selectCollection('dbName', 'collName');
$collection->insertOne(['foo' => 'bar']);
var_dump($collection->findOne(['foo' => 'bar']));

This small snippet creates a connection, inserts a sample record into a collection via insertOne, then retrieves this record using the findOne.

To answer your other questions:

  1. The PECL extension and composer library are client libraries. No database server is provided by the driver
  2. You can either run a server locally, or you can create a free cluster on MongoDB Atlas.
  3. Databases are created on-demand as they are used.
  4. The example above shows how to use insertOne. The CRUD tutorial in the documentation shows some of the other methods you can use to write and read data in your database.

Out of curiosity and to potentially improve our documentation, did you follow any guides to get to the current point? I’m wondering because in our docs we always recommend people use the high-level library, since the PECL extension only provides a low-level interface not suited for everyday use.

Thanks,
Andreas

Hi Andreas,
thanks for your long answer!
I did start with a blank ubuntu 20.04 image:

apt update
apt install apache2
add-apt-repository ppa:ondrej/php
apt install php8.0
service apache2 restart

apt-get install php-pear
apt-get install php-dev (to avoid error "phpize not found")
apt install pecl

pecl install mongodb
apt install composer
added extension=mongodb.so to php.ini and it shows it with phpinfo();

cd /var/www/html
composer require mongodb/mongodb
Error from composer: 
Problem 1
    - Installation request for mongodb/mongodb ^1.15 -> satisfiable by mongodb/mongodb[1.15.0].
    - mongodb/mongodb 1.15.0 requires ext-mongodb ^1.15.0 -> the requested PHP extension mongodb is missing from your system.

More or less the same when I install php8.2

It does not know the class MongoDB\Client :confused:

Any other way to install mongodb with php8.2 ?

Suggestion: apt install php8.2-mongodb would be nice :slight_smile:

And at the end: Working with the DB will be a pleasure, I still like it.
It’s fast and JSON is quite easy to handle so I’m looking forward to it!

Another idea: At the end I will have a mongodb server on one of my instances.
Is the basic stuff from pecl enough to connect to that local mongodb server or do I need also the library ?

Best and again thanks in advance
Robert

The error from composer indicates that the extension is not added to the CLI environment. You can check whether it’s installed by running php --ri mongodb, which should show you information about the extension. Be aware that Ubuntu may have different php.ini files for the CLI and FPM SAPI.

Theoretically speaking, the PECL extension is enough to connect to MongoDB, but it does not provide you with a high level API. For example, to insert a document you’ll have to manually create a bulk write, add the appropriate operations to it, and execute it on the manager or a previously selected server. I would always recommend using the PHP library along with the extension for a better development experience.

Hi Andreas,
got it finally to run with a help of a friend.
I was not sure if a use a local file system or if I’m on my local server.

There is the local server and the atlas and nothing more, right ?
So we need:

  1. Install the extension
  2. Install the library
  3. Install the local server

Hereby is a little program that could be helpful at the beginning of your php tutorial.
The examples given are to specific in my opinion. If you are used to it they might be fine but to get in touch mongodb frustrated my a lot!

<?php

echo "Mongo DB Example<br>";

// run composer require mongodb/mongodb in your project folder before
require("vendor/autoload.php");

// start mongod process on your instance first
$client = new MongoDB\Client('mongodb://localhost:27017');

// select a database (will be created automatically if it not exists)
$db = $client->test2;
echo "Database test2 created/selected<br>";

// select a collection (will be created automatically if it not exists)
$coll = $db->selectCollection("mycoll");
echo "Collection mycoll created/selected<br>";

// insert a datatow in the collection
$coll->insertOne(['foo' => 'bar']);

// search for the datarow
echo "Result:<br>";
var_dump($coll->findOne(['foo' => 'bar']));

?>

At the end: Can you recommend any editor with mongodb php intellisense supported or installable ?

Thanks for your help!

Best Robert

You can deploy MongoDB on any system, just like you would on the local system. For production use you’d usually want something more resilient than a standalone: for operational resilience (read, protection against data loss) you’d create a replica set, and for scaling you’d create a sharded cluster. Atlas merely takes the task of managing your own servers off your hands.

Any editor with code completion should be able to offer completion for the methods in the PHP library - the extension needs stub files for code completion to work (PhpStorm for example provides those). I personally use PhpStorm for my development needs and haven’t tried other editors for any significant work.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.