doctrine/mongodb
Doctrine MongoDB library for PHP, providing an object-oriented API for working with MongoDB. Includes database and collection abstractions, query and command builders, and tools for common CRUD operations—ideal as a low-level MongoDB foundation for PHP apps.
Installation
composer require doctrine/mongodb:^1.6.4
(Note: This package is archived. For new projects, consider jenssegers/laravel-mongodb or the official mongodb/mongodb driver. Ensure PHP 7.4+ and MongoDB 4.0+ compatibility.)
Configure MongoDB Connection
Add a MongoDB connection in config/database.php:
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_MONGODB_HOST', '127.0.0.1'),
'port' => env('DB_MONGODB_PORT', 27017),
'database' => env('DB_MONGODB_DATABASE', 'laravel'),
'username' => env('DB_MONGODB_USERNAME'),
'password' => env('DB_MONGODB_PASSWORD'),
'options' => [
'connect' => env('DB_MONGODB_CONNECT', false), // Set to `false` in production
],
],
],
First Query
Use the MongoDBManager facade to interact with collections:
use Doctrine\MongoDB\MongoDBManager;
$manager = MongoDBManager::connection('mongodb');
$collection = $manager->selectCollection('users');
$user = $collection->findOne(['email' => 'test@example.com']);
Eloquent Integration (Optional)
For Eloquent-like syntax, use jenssegers/laravel-mongodb (recommended):
composer require jenssegers/laravel-mongodb
CRUD Operations
// Insert
$collection->insertOne(['name' => 'John', 'email' => 'john@example.com']);
// Update with `replaceRoot` (fixed in 1.6.4)
$collection->updateOne(
['email' => 'john@example.com'],
['$set' => ['name' => 'John Doe']]
);
// Delete
$collection->deleteOne(['email' => 'john@example.com']);
Aggregation Pipeline (Fixed replaceRoot Stage)
$pipeline = [
['$match' => ['status' => 'active']],
['$replaceRoot' => ['newRoot' => ['$mergeObjects' => ['$$ROOT', { 'activeSince' => '$createdAt' }]]]], // Now works correctly
['$group' => ['_id' => '$department', 'count' => ['$sum' => 1]]],
];
$results = $collection->aggregate($pipeline);
Transactions (Multi-Document)
$session = $manager->createSession();
$session->startTransaction();
try {
$collection1->insertOne([...], ['session' => $session]);
$collection2->insertOne([...], ['session' => $session]);
$session->commitTransaction();
} catch (\Exception $e) {
$session->abortTransaction();
}
Index Management
$collection->createIndex(['email' => 1], ['unique' => true]);
MongoDBManager to the container:
$this->app->singleton('mongodb', function ($app) {
return MongoDBManager::connection('mongodb');
});
Doctrine\MongoDB\Query\Query for complex queries:
$query = new Query();
$query->where('age')->gt(18);
$results = $collection->find($query);
$hydrator = new \Doctrine\MongoDB\Hydrator\ObjectHydrator();
$user = $hydrator->hydrate($collection->findOne([...]), User::class);
jenssegers/laravel-mongodb or the official driver.replaceRoot Stage: Fixed in 1.6.4, but test thoroughly if using aggregation pipelines with this stage.options['connect'] to false in production to avoid overhead.$manager->getClient()->setLoggerLevel(\MongoDB\Driver\Logger::DEBUG);
json_encode($document, JSON_PRETTY_PRINT).replaceRoot syntax).\Doctrine\MongoDB\Hydrator\HydratorInterface for custom mapping.MongoDBManager for pre/post-query logic.$manager->getClient()->executeQuery('admin.$cmd', ['ping' => 1]);
MongoDB\BSON\Type\Type for custom BSON types.replaceRoot) for complex transformations.How can I help you explore Laravel packages today?