embedding-mongodb-driver maintained by x-laravel
x-laravel/embedding — MongoDB Driver
⚠️ This package has been cancelled and is not functional. See CLAUDE.md for details.
MongoDB vector driver for x-laravel/embedding.
How It Works
- Provides
MongodbEmbedding— a MongoDB-native Eloquent model that stores vectors as BSON arrays, reads the collection name fromembedding.database.tableconfig - Implements
SimilarityDriver— registers as themongodbdriver for MongoDB Atlas Vector Search using$vectorSearchaggregation - Community MongoDB (without Atlas): vectors are stored natively as BSON arrays; similarity search falls back to
PhpDriverautomatically
Requirements
- PHP ^8.3 +
ext-mongodb - Laravel ^12.0 | ^13.0
x-laravel/embedding ^1.0mongodb/laravel-mongodb ^4.0- MongoDB 8.0+ (community) or MongoDB Atlas (for native vector search)
Installation
composer require x-laravel/embedding-mongodb-driver
The MongodbEmbeddingServiceProvider is auto-discovered and registers the mongodb driver automatically.
Setup
1. Configure x-laravel/embedding
Publish the config if you haven't already:
php artisan vendor:publish --tag=embedding-config
Set the MongoDB connection and swap the Embedding model in config/embedding.php:
'database' => [
'connection' => env('EMBEDDINGS_DATABASE_CONNECTION', 'mongodb'),
'table' => env('EMBEDDINGS_DB_TABLE', 'embeddings'),
],
// Swap the default SQL model with the MongoDB-native model
'model' => \XLaravel\Embedding\Driver\Mongodb\Models\MongodbEmbedding::class,
'similarity' => [
'driver' => env('EMBEDDING_SIMILARITY_DRIVER', 'auto'),
],
2. Configure the MongoDB connection
In config/database.php:
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'myapp'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
],
3. Create the embeddings collection
php artisan migrate
Or publish the migration first:
php artisan vendor:publish --tag=embedding-mongodb-migrations
php artisan migrate
This creates a unique index on (embeddable_type, embeddable_id, slot).
4. MongoDB Atlas Vector Search (optional)
For native DB-level similarity search, create a Vector Search index on the embeddings collection in Atlas with path vector and set the similarity driver:
'similarity' => ['driver' => 'mongodb'],
Note: Without Atlas, similarity search automatically uses
PhpDriver(vectors loaded into PHP memory). For large datasets, Atlas Vector Search is strongly recommended.
5. Model
Follow the standard x-laravel/embedding setup. No MongoDB-specific changes are needed on your models.
use XLaravel\Embedding\Attributes\EmbedOn;
use XLaravel\Embedding\Concerns\Embeddable;
use XLaravel\Embedding\Contracts\HasEmbeddings;
#[EmbedOn(['title', 'body'])]
class Post extends Model implements HasEmbeddings
{
use Embeddable;
public function toEmbeddingText(): string
{
return $this->title.' '.$this->body;
}
}
Usage
The driver is transparent — use the standard x-laravel/embedding API:
Post::similarToText('web framework', limit: 10);
Post::similarTo($vector, limit: 10, threshold: 0.8);
Post::rankByRelevance($posts, 'web framework');
$post->mostSimilar(limit: 5);
$post->similarityTo($otherPost);
All methods set a similarity_score float attribute on each returned model.
Testing
# Build first (once per PHP version)
DOCKER_BUILDKIT=0 docker compose --profile php83 build
# Run tests
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up
License
This package is open-sourced software licensed under the MIT license.