PHP Velho Oeste 2024

Драйвер MongoDB

Этот модуль разработан на основе библиотек » libmongoc и » libbson. Он предоставляет минимальное API для ключевого функционала драйвера: команды, запросы, записи, управление соединением и сериализация BSON.

Самодельные библиотеки PHP, требующие этот модуль, могут предоставлять высокоуровневые API, такие как: сборщики запросов, методы-помощники для индивидуальных команд и GridFS. Разработчики приложений должны рассмотреть вопрос об использовании этого модуля совместно с » библиотекой MongoDB PHP, которая реализует такие же высокоуровневые API драйвера MongoDB, как и для других языков. Подобное разделение задач позволяет этому драйверу сконцентрироваться на главных задачах, стоящих перед ним - повышение производительности.

add a note

User Contributed Notes 4 notes

n dot vandermeij at mycademy dot com
7 years ago
*** ONLY FOR VERSIONS >= 1.2.0 ***

If you encounter the following error:

"PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/'
- /usr/lib64/php/modules/ undefined symbol: php_json_serializable_ce in Unknown on line 0"

For a detailed explanation, please visit:

TLDR: You need to load the extension after the

Special thanks to Derick Rethans for pointing this out!
mike at eastghost dot com
6 years ago
There is an adapter - so old MongoClient / MongoDB code will run on the new PHP7/Mongo mess
mike at eastghost dot com
6 years ago

In new driver, these fail:

(A) $count = $collection->find( $criteria )->count();

(B1) $cursor = $collection->find( $criteria );
(B2) $count = $cursor->count();

In new driver, result counting has become its own separate action, to be applied to the collection, not the cursor:

$count = $collection->count( $criteria );
$cursor = $collection->find( $criteria );

From jmikola 6 Jan 2016 ([url] here[/url]):

"In the new driver, a Cursor object now represents the actual results of an executed command or query, whereas cursors in the legacy driver had a dual-nature (pre- or post-executed). The legacy MongoCursor::count() method actually invoked a count command with the same criteria; however, this meant it was entirely possible for the count command's calculation to differ from the actual query results, since they were not necessarily operating on the same result data.

"The userland library implements a count() method on the collection, which also takes filter criteria, and we'd rather encourage that so users understand that the operations are separate. If we do end up making Cursor countable in the userland library, we would use iterator_count() to provide an exact count on the results; however, this will also require us to cache the results so that users can rewind the cursor and iterate again (this was most recently discussed in PHPLIB-81). While the legacy driver would re-execute the query on a rewind, that's also not an option given the nature of a Cursor in the new driver.

"I realize this may come across as an inconvenience, but I believe the explicit API is a ultimately a step forward from the legacy driver, where many users might have been oblivious to what was actually happening under the hood. Feel free to follow up if you have additional questions.
11 months ago
Hey guys,
a short example to access a local mongo db server:


echo "Mongo DB Example<br>";

// run composer requre mongodb/mongdodb 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;
"Database test2 created/selected<br>";

// select a collection (will be created automatically if it not exists)
$coll = $db->selectCollection("mycoll");
"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']));

To Top