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:
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:
The PECL extension and composer library are client libraries. No database server is provided by the driver
You can either run a server locally, or you can create a free cluster on MongoDB Atlas.
Databases are created on-demand as they are used.
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 for your long answer!
I did start with a blank ubuntu 20.04 image:
apt install apache2
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();
composer require mongodb/mongodb
Error from composer:
- 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
Any other way to install mongodb with php8.2 ?
Suggestion: apt install php8.2-mongodb would be nice
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 ?
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.
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:
Install the extension
Install the library
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!
echo "Mongo DB Example<br>";
// run composer require mongodb/mongodb in your project folder before
// 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
var_dump($coll->findOne(['foo' => 'bar']));
At the end: Can you recommend any editor with mongodb php intellisense supported or installable ?
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.