PHPackages                             x-laravel/embedding-mongodb-driver - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. x-laravel/embedding-mongodb-driver

ActiveLibrary[Database &amp; ORM](/categories/database)

x-laravel/embedding-mongodb-driver
==================================

MongoDB native vector driver for x-laravel/embedding.

00PHPCI failing

Since May 9Pushed 1mo agoCompare

[ Source](https://github.com/x-laravel/embedding-mongodb-driver)[ Packagist](https://packagist.org/packages/x-laravel/embedding-mongodb-driver)[ RSS](/packages/x-laravel-embedding-mongodb-driver/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

x-laravel/embedding — MongoDB Driver
====================================

[](#x-laravelembedding--mongodb-driver)

[![Tests](https://github.com/x-laravel/embedding-mongodb-driver/actions/workflows/tests.yml/badge.svg)](https://github.com/x-laravel/embedding-mongodb-driver/actions/workflows/tests.yml)[![PHP](https://camo.githubusercontent.com/c8d8dad6beb757a2b8acba331d16140813699543b88a37af0a81f20bd35f61de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332532422d626c7565)](https://www.php.net)[![Laravel](https://camo.githubusercontent.com/42e62a9adb05b6cb16993782fd4b04b64a76be3ff5704d170001885eb70c8448/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d313225323025374325323031332d726564)](https://laravel.com)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE.md)

> **⚠️ This package has been cancelled and is not functional.** See [CLAUDE.md](CLAUDE.md) for details.

MongoDB vector driver for [x-laravel/embedding](https://github.com/x-laravel/embedding).

How It Works
------------

[](#how-it-works)

- Provides `MongodbEmbedding` — a MongoDB-native Eloquent model that stores vectors as BSON arrays, reads the collection name from `embedding.database.table` config
- Implements `SimilarityDriver` — registers as the `mongodb` driver for [MongoDB Atlas Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/) using `$vectorSearch` aggregation
- Community MongoDB (without Atlas): vectors are stored natively as BSON arrays; similarity search falls back to `PhpDriver` automatically

Requirements
------------

[](#requirements)

- PHP ^8.3 + `ext-mongodb`
- Laravel ^12.0 | ^13.0
- `x-laravel/embedding ^1.0`
- `mongodb/laravel-mongodb ^4.0`
- MongoDB 8.0+ (community) or MongoDB Atlas (for native vector search)

Installation
------------

[](#installation)

```
composer require x-laravel/embedding-mongodb-driver
```

The `MongodbEmbeddingServiceProvider` is auto-discovered and registers the `mongodb` driver automatically.

Setup
-----

[](#setup)

### 1. Configure x-laravel/embedding

[](#1-configure-x-laravelembedding)

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

[](#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

[](#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)

[](#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

[](#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
-----

[](#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
-------

[](#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
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/license/MIT).

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance61

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bdb64c6c087c331b8bd5906bb1aa7eb06bc83af3654a48ba8ab9da365976651?d=identicon)[X-Adam](/maintainers/X-Adam)

---

Top Contributors

[![x-adam](https://avatars.githubusercontent.com/u/60411758?v=4)](https://github.com/x-adam "x-adam (2 commits)")

### Embed Badge

![Health badge](/badges/x-laravel-embedding-mongodb-driver/health.svg)

```
[![Health](https://phpackages.com/badges/x-laravel-embedding-mongodb-driver/health.svg)](https://phpackages.com/packages/x-laravel-embedding-mongodb-driver)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8361.6M87](/packages/propel-propel1)[mpociot/laravel-composite-key

Support composite keys in your laravel app.

3544.8k1](/packages/mpociot-laravel-composite-key)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
