mongodb/laravel-mongodb
MongoDB integration for Laravel Eloquent and the Query Builder, extending the native Laravel API to work with MongoDB. Official mongodb/laravel-mongodb package (formerly jenssegers), compatible with Laravel 10.x.
$lookup), but performance degrades for complex hybrid operations due to MongoDB’s lack of native SQL-like joins. Key trade-off: Flexibility over ACID compliance.ModelObserver) for MongoDB operations, enabling seamless integration with queues, notifications, and real-time updates.DatabaseTransactionsManager changes in v5.6.0).mongodb/mongodb). Risk: Driver version mismatches may break operations (e.g., transactions, aggregation pipelines).User::where('age', '>', 25)->get()), but indexing strategies must shift from SQL to MongoDB (e.g., compound indexes, text indexes).php artisan migrate with custom MongoDB migrations (e.g., Schema::createCollection()) or leverage Laravel’s schema builder for hybrid setups._id (ObjectId), while Eloquent defaults to incremental IDs. Mitigation: Configure protected $primaryKey = '_id' and protected $keyType = 'string' in models.with()) works but may generate inefficient $lookup stages. Solution: Use load() or batch loading for related documents.MongoBatchRepository::retry()).$inc, $push)?$out, $facet)?Illuminate\Database\Eloquent\Model with Jenssegers\Mongodb\Eloquent\Model.MongoDB\Laravel\Eloquent\Builder for MongoDB-specific queries (e.g., $text, $geoNear).Illuminate\Database\Migrations\Migrator with MongoDB support (e.g., Schema::createCollection()).MongoDB\Laravel\Testing\CreatesCollections for test fixtures.create() and factory().JOIN with $lookup, GROUP BY with $group).user).{ email: 1 }, { created_at: -1, status: 1 }).Jenssegers\Mongodb\Eloquent\Model.config/database.php:
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'connect' => true,
],
],
],
users table to a MongoDB collection with embedded posts:
// SQL: users table with posts in a separate table
// MongoDB: users collection with posts embedded
$users = User::all()->each(function ($user) {
$user->posts = Post::where('user_id', $user->id)->get();
$user->save(); // Uses MongoDB's $push for nested documents
});
| Feature | MongoDB Support | Notes |
|---|---|---|
| Eloquent Relationships | Yes (hasOne, hasMany, belongsTo) | Uses $lookup under the hood. |
| Soft Deletes | Yes (via SoftDeletes trait) |
Deprecated in v5.5.0; use $set. |
| Scopes | Yes | Custom scopes can use MongoDB operators. |
| Accessors/Mutators | Yes | Works as in SQL Eloquent. |
| Events | Yes | created, updated, etc. |
| Transactions | Partial (multi-doc, no cross-collection) | Use MongoBatchRepository. |
| Caching | Yes (via cache() method) |
Uses Redis or file cache. |
| File Storage | Yes (via Storage facade) |
Works with S3, local, etc. |
Model::aggregate() or raw `$collection->How can I help you explore Laravel packages today?