alcaeus/mongo-php-adapter
Compatibility layer that lets legacy PHP MongoDB drivers (ext-mongo) work with the newer mongodb extension and library. Helps modernize apps with minimal code changes by translating old APIs to the current MongoDB PHP driver.
Installation:
composer require alcaeus/mongo-php-adapter:^1.2.5
Ensure ext-mongodb (PHP driver) is installed (php -m | grep mongodb) and PHP 8.2 is used (this release fixes compatibility).
Basic Usage (Updated for PHP 8.2):
use MongoDB\Client as MongoClient;
use Alcaeus\MongoClient\Connection;
$client = new MongoClient('mongodb://localhost:27017');
$adapter = new Connection($client); // Now fully PHP 8.2 compatible
$collection = $adapter->selectCollection('db', 'collection');
First Use Case (Legacy Migration):
Replace deprecated ext-mongo calls with the adapter:
// Old (deprecated)
$conn = mongo_connect('localhost');
// New (PHP 8.2 safe)
$client = new MongoClient('mongodb://localhost');
$adapter = new Connection($client);
Wrap MongoDB\Client (No Breaking Changes):
$mongoClient = new MongoClient('mongodb://user:pass@host:port');
$legacyAdapter = new Connection($mongoClient); // Fixed for PHP 8.2
Replace mongo_* Functions (Updated Examples):
mongo_connect() → new Connection($client)mongo_query() → $collection->find($query) (returns Cursor object)Cursor Handling (No Changes Needed):
$cursor = $collection->find(['status' => 'active']);
while ($cursor->advance()) {
$doc = $cursor->current(); // Works as before
}
jenssegers/mongodb (tested with PHP 8.2):
$adapter = new Connection(new MongoClient(env('DB_CONNECTION')));
config(['database.connections.mongodb.adapter' => $adapter]);
$result = $collection
->find()
->limit(10)
->sort(['createdAt' => -1]);
$session = $client->startSession();
$adapter->setSession($session); // Now works without private API issues
Deprecation Warning (Still Applies):
The package is deprecated (last release 2023). Prefer native mongodb/mongodb for new projects.
ext-mongo.PHP 8.2 Specific Fixes:
foreach with string keys).Method Mismatches (Unchanged):
mongo_insert() → Use $collection->insertOne().mongo_find() → Use $collection->find() (returns Cursor).Session Handling (Fixed in 1.2.5):
$session = $client->startSession();
$adapter->setSession($session); // Safe for transactions
$adapter = new Connection($client, [
'logger' => new \Monolog\Logger('mongo_adapter'),
]);
xdebug to trace Alcaeus\MongoClient method calls in legacy code.
Connection for domain logic (PHP 8.2 compatible):
class CustomAdapter extends Connection {
public function findByEmail($email) {
return $this->selectCollection('users')->find(['email' => $email]);
}
}
Cursor (no PHP 8.2 issues):
class CachedCursor extends Cursor {
protected $cache = [];
public function current() {
if (!isset($this->cache[$this->key])) {
$this->cache[$this->key] = parent::current();
}
return $this->cache[$this->key];
}
}
$query = ['$query' => ['age' => ['$gt' => 18]], '$orderby' => ['name' => 1]];
$result = $collection->find($query);
How can I help you explore Laravel packages today?