mongodb/laravel-mongodb
MongoDB integration for Laravel Eloquent and query builder. Extends core Laravel classes to use the same familiar API with MongoDB. Official mongodb/laravel-mongodb package, compatible with Laravel 10.x; docs and releases maintained by MongoDB.
Model::where(), Model::find(), relationships).$lookup in aggregations).extends Model with extends \MongoDB\Laravel\Eloquent\Model (or Document for pure MongoDB models).where(), orWhere(), aggregations, and MongoDB-specific operators (e.g., $regex, $elemMatch).Schema::create() with MongoDB-specific options (e.g., index(), unique(), ttl()).scout driver.db:show, db:tables, migrate, schema:dump (with MongoDB-specific extensions).MongoDB\Database\MockManager or Laravel’s DatabaseMigrations.| Risk Area | Severity | Mitigation |
|---|---|---|
| Schema Design | High | Requires upfront modeling of document structures; denormalization may be needed. |
| Performance | Medium | Indexing critical; avoid over-fetching (use select() or projections). |
| Transactions | Medium | Limit transaction scope; avoid long-running or parallel operations. |
| Driver Compatibility | Low | Pin mongodb/mongodb driver version to avoid breaking changes. |
| Legacy Code | Medium | Deprecated features (e.g., soft deletes) may require refactoring. |
| Debugging | Medium | MongoDB’s error messages differ from SQL; use dd() or explain() for queries. |
mongodb (PECL extension for MongoDB driver).php-redis (if using MongoDB sessions or caching).mongostat, mongotop for performance tuning.| Phase | Actions | Tools/Libraries |
|---|---|---|
| Assessment | Audit existing Eloquent models for MongoDB compatibility; identify relational dependencies. | phpstan, custom scripts to analyze queries. |
| Pilot | Migrate non-critical modules (e.g., logs, analytics) to MongoDB. | Laravel’s Model → Document swaps. |
| Hybrid Integration | Use Laravel’s database connections to route models to SQL/MongoDB. | config/database.php connection configs. |
| Full Migration | Replace SQL models with MongoDB documents; update migrations. | Schema::create(), php artisan migrate. |
| Optimization | Add indexes, optimize queries, and test performance under load. | explain(), mongodump/mongorestore. |
DB::select('RAW SQL') with joins).laravel-excel, spatie/laravel-permission).laravel-debugbar for query visualization).DatabaseMigrations for test isolation.MongoDB\Database\MockManager for unit tests..env:
DB_CONNECTION=mongodb
MONGO_DB=your_db
MONGO_HOST=127.0.0.1
MONGO_PORT=27017
use Illuminate\Database\Eloquent\Model with:
use MongoDB\Laravel\Eloquent\Model as MongoModel;
class User extends MongoModel { ... }
Schema::create('users', function (Blueprint $collection) {
$collection->index('email');
$collection->index('name');
$collection->ttl('expires_at');
});
// SQL: WHERE status = 'active' AND created_at > NOW() - INTERVAL 1 DAY
User::where('status', 'active')
->where('created_at', '>', now()->subDay())
->get();
User::aggregate([
['$match' => ['status' => 'active']],
['$group' => ['_id' => '$department', '
How can I help you explore Laravel packages today?