Model::where(), hasMany(), relationships). Minimal refactoring needed for basic CRUD.$lookup, $facet, $expr) while maintaining Laravel’s fluent syntax.robmorgan/phinx with custom adapters.changeStreams for schema evolution tracking.| Risk Area | Mitigation Strategy |
|---|---|
| Data Consistency | MongoDB’s eventual consistency may conflict with Laravel’s transactional expectations. Use multi-document ACID transactions (MongoDB 4.0+) sparingly. |
| Performance | Indexing strategy critical—poorly designed queries (e.g., no indexes on where clauses) lead to collection scans. Profile with explain("executionStats"). |
| Tooling Gaps | Lack of Laravel Scout support for MongoDB (use Algolia or custom full-text search). Tinker/Artisan works but lacks SQL-like introspection. |
| Vendor Lock-in | MongoDB Atlas (SaaS) vs. self-hosted tradeoffs. Driver version alignment (e.g., mongodb/mongodb PHP driver) must match Laravel package. |
| Testing | Mocking MongoDB in PHPUnit requires mockery or predis/mock alternatives. Consider Dockerized MongoDB for CI. |
hasMany) be modeled? MongoDB favors embedded documents over joins.laravel-snappy for exports).ext-mongodb (official driver) or ext-mongo (legacy, unsupported).mongodb/mongodb driver (v1.15+) for performance.symfony/console, symfony/http-foundation (already in Laravel).spatie/laravel-permission works if using MongoDB models).Jenssegers\Mongodb\Eloquent\Model.DB::collection('users')->where(...)).UserRepository with SQL/MongoDB implementations).interface UserRepository { find(int $id); }
class MongoUserRepository implements UserRepository { ... }
| Feature | Compatibility Notes |
|---|---|
| Eloquent | Full support (relationships, events, observers). Polymorphic and many-to-many work but may require custom logic. |
| Migrations | Not supported. Use manual scripts or third-party tools (e.g., doctrine/dbal with MongoDB). |
| Queues | Works with Laravel queues, but job persistence relies on MongoDB’s durability. |
| Caching | Integrates with Laravel cache, but cache invalidation must be manual (e.g., Cache::forget() on model events). |
| Scout | No native support. Use MongoDB Atlas Search or external search (Elasticsearch). |
| First-Party Pkgs | Laravel Nova: Limited support (custom development needed). Laravel Forge: No direct integration. |
pecl install mongodb..env:
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Jenssegers\Mongodb\Eloquent\Model:
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model { ... }
User::where() with DB::collection('users')->where() for complex queries.explain() to validate query plans.DB::transaction().mongodump in CI/CD).creating hook to add indexes).mongodb/mongodb driver may break compatibility. Pin versions in composer.json.$lookup failures lack clear error messages).putenv('MONGODB_LOG_LEVEL=2'); // Verbose
socketTimeoutMS in connection string.limit() and skip() judiciously.How can I help you explore Laravel packages today?