- How do I install mongodb/laravel-mongodb in a Laravel 10.x project?
- Run `composer require mongodb/laravel-mongodb` in your project root. The package auto-discovers Laravel’s service container, so no manual configuration is needed for basic usage. Ensure your PHP environment has the MongoDB PHP extension (mongodb) installed and enabled.
- Does mongodb/laravel-mongodb support Laravel’s migrations and seeding?
- Yes, the package integrates fully with Laravel’s migration system. Use `Schema::create()` with MongoDB-specific syntax (e.g., `createCollection()`) and seed data via `DatabaseSeeder` or model factories. Example: `php artisan migrate` works as usual, but collections replace tables.
- Can I use Eloquent relationships (e.g., hasMany, belongsTo) with MongoDB?
- Most Eloquent relationships work, but complex ones like `hasManyThrough` may require custom logic due to MongoDB’s lack of native joins. For nested data, prefer embedded documents or `$lookup` aggregations. Always test relationships in your specific use case.
- What Laravel versions does mongodb/laravel-mongodb support?
- The current version (as of 2023) is optimized for Laravel 10.x. Older versions (e.g., Laravel 8/9) are supported via tagged releases (e.g., `3.9.x`). Check the [GitHub releases](https://github.com/mongodb/laravel-mongodb/releases) for compatibility matrices.
- How do I handle transactions with mongodb/laravel-mongodb?
- Multi-document ACID transactions are supported via `DB::transaction()`. However, transactions have limitations: they don’t span shards, and nested transactions aren’t allowed. Use them for critical operations like financial data but avoid overuse for performance reasons.
- Is mongodb/laravel-mongodb suitable for production workloads with high write throughput?
- It depends on your schema design. MongoDB excels at high-throughput reads and unstructured data, but writes can become slow if documents grow too large or lack proper indexing. Test with realistic data volumes and monitor performance metrics like `insert`/`update` latency.
- How do I create indexes in mongodb/laravel-mongodb?
- Use the `createIndex()` method on your model or define indexes in migrations via `Schema::createCollection()` with `index()`. Example: `$model->createIndex('email', ['unique' => true])`. For geospatial or text indexes, use MongoDB’s syntax (e.g., `2dsphere` for geospatial).
- What alternatives exist to mongodb/laravel-mongodb for Laravel + MongoDB?
- For Laravel, alternatives include `jenssegers/laravel-mongodb` (legacy, unmaintained) or raw MongoDB PHP driver with custom query logic. For other frameworks, consider `mongodb/mongodb` (official driver) or `doctrine/mongodb-odm`. However, none match `mongodb/laravel-mongodb`’s Eloquent integration depth.
- How do I debug slow queries in mongodb/laravel-mongodb?
- Use MongoDB’s `explain()` method on queries: `$model->where(...)->explain()`. For Laravel Tinker, enable debug mode with `DB::enableQueryLog()`. Tools like MongoDB Compass or `mongostat` can also analyze performance. Avoid `EXPLAIN` in production due to overhead.
- Can I use mongodb/laravel-mongodb with Laravel Scout for full-text search?
- Yes, the package integrates with Laravel Scout. Configure your Scout engine to use MongoDB’s text indexes (e.g., `createIndex('content', ['text' => 1])`) and set the `searchableAs` trait on your model. Scout will handle the rest, including pagination and ranking.