- How do I install mongodb/laravel-mongodb in a Laravel 10.x project?
- Run `composer require mongodb/laravel-mongodb` in your project directory. The package auto-discovers Laravel and integrates with Eloquent and Query Builder without additional configuration. Ensure your `config/database.php` includes a MongoDB connection.
- Can I use Eloquent relationships (e.g., hasMany) with MongoDB documents?
- Yes, the package supports Laravel’s standard relationships like `hasOne`, `hasMany`, and polymorphic relationships. For embedded documents, use `hasOne` with `embed()` or `embedOne()` methods. One-to-many relationships work but may require denormalization for complex queries.
- Does mongodb/laravel-mongodb support Laravel Scout for full-text search?
- Yes, the package integrates with Scout using MongoDB’s Atlas Search or the legacy Scout driver. Configure your `config/scout.php` to use the MongoDB engine, and leverage MongoDB’s advanced text search capabilities like fuzzy matching and synonyms.
- What Laravel versions does mongodb/laravel-mongodb officially support?
- The package officially supports Laravel 10.x–13.x (as of v5.7.0). For older versions (e.g., Laravel 9.x), use v4.x or earlier releases. Check the [GitHub releases](https://github.com/mongodb/laravel-mongodb/releases) for version-specific compatibility notes.
- How do I handle soft deletes in MongoDB with this package?
- Soft deletes were deprecated in v5.x. Use MongoDB’s TTL indexes for automatic document expiration or implement custom logic with `deleted_at` fields. For existing models, migrate to TTL indexes via `Schema::create()` or manual queries.
- Can I use MongoDB transactions with this package?
- Yes, the package supports MongoDB’s multi-document ACID transactions. Use Laravel’s `DB::transaction()` or the Query Builder’s `getMongoClient()->startSession()`. Note transactions have performance trade-offs; avoid long-running or parallel operations.
- What are the performance considerations for high-write workloads?
- MongoDB excels at high-write throughput (e.g., logs, analytics). Optimize performance by indexing frequently queried fields, using bulk writes (`insertMany`, `updateMany`), and avoiding over-fetching. For >10K writes/sec, consider sharding or Atlas clusters.
- How do I test MongoDB models in Laravel’s PHPUnit tests?
- Use Laravel’s `DatabaseMigrations` or MongoDB’s `MockManager` to mock the database. For migrations, run `php artisan migrate:fresh` in tests. Example: `$this->actingAs($user)->get('/posts')->assertOk();` with a test database connection.
- Are there alternatives to mongodb/laravel-mongodb for Laravel + MongoDB?
- Other options include `jenssegers/laravel-mongodb` (older, less maintained) or building custom Eloquent extensions. However, `mongodb/laravel-mongodb` is the official, actively maintained choice with full Laravel 10.x+ support and MongoDB’s backing.
- How do I verify the integrity of the package’s releases?
- MongoDB signs release tags with their GPG key. Import the key (`gpg --import php-driver.asc`) and verify tags locally with `git show --show-signature <tag>`. While Composer doesn’t natively support this, it’s a manual safeguard against tampered releases.