PHPackages                             liquidbox/silex-mongodb - 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. [Framework](/categories/framework)
4. /
5. liquidbox/silex-mongodb

ActiveLibrary[Framework](/categories/framework)

liquidbox/silex-mongodb
=======================

A MongoDB service provider for the Silex micro-framework

06PHP

Since Jan 4Pushed 8y ago1 watchersCompare

[ Source](https://github.com/liquidbox/silex-mongodb)[ Packagist](https://packagist.org/packages/liquidbox/silex-mongodb)[ RSS](/packages/liquidbox-silex-mongodb/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

[![GitHub release](https://camo.githubusercontent.com/9b94da244672a7e5139f4e725759a8c9e26852715eb70452aa675332aaf0c6fa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6c6971756964626f782f73696c65782d6d6f6e676f64622e737667)](https://github.com/liquidbox/silex-mongodb/releases)[![license](https://camo.githubusercontent.com/fc4cca5ef5bea5e5b3fb5163fd193f7b61890129b217e07db44e77830b274926/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6971756964626f782f73696c65782d6d6f6e676f64622e737667)](LICENSE)[![Build Status](https://camo.githubusercontent.com/9850f97b746fa59ca329c15d924c9da946604a1acdeb1c2d33a8fc9e8a6e2253/68747470733a2f2f7472617669732d63692e6f72672f6c6971756964626f782f73696c65782d6d6f6e676f64622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/liquidbox/silex-mongodb)[![Code Coverage](https://camo.githubusercontent.com/8340e1d45ad8924745a0d614e00a2dfe80acfa39eb9cc558a2f14e1f0c040485/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6971756964626f782f73696c65782d6d6f6e676f64622f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/liquidbox/silex-mongodb/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e7c2ba89ecc0d23eba2cac863a625368e15f18204dc66edb9881c5626cfa225f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6971756964626f782f73696c65782d6d6f6e676f64622f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/liquidbox/silex-mongodb/?branch=master)[![Packagist](https://camo.githubusercontent.com/f8d9ad242f84a9d8fefe098c6481f5b5f8fa99212b2510bcf4f529a753c1671d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6971756964626f782f73696c65782d6d6f6e676f64622e737667)](https://packagist.org/packages/liquidbox/silex-mongodb)

You are reading the documentation for Silex 2.x. Switch to the documentation for Silex [1.x](../v1.0.0/README.md).

MongoDB
=======

[](#mongodb)

The *MongoDbServiceProvider* provides integration with the [MongoDB](http://php.net/manual/set.mongodb.php) extension.

Parameters
----------

[](#parameters)

- **mongodb.uri** (optional): A [MongoDB](https://docs.mongodb.com/manual/reference/connection-string) connection URI.
- **mongodb.connection** (optional): A collection of parameters for specifying the connection string.
    - **host** (optional): The server address to connect to. Identifies either a hostname, IP address, or UNIX domain socket.
    - **port** (optional): The default value is 27017.
    - **username** (optional): The username for the connection string.
    - **password** (optional): The password for the connection string.
    - **database** (optional): The name of the database.
    - **options** (optional): A collection of connection specific options. See [Connection String Options](https://docs.mongodb.com/manual/reference/connection-string/index.html#connections-connection-options) for a full description of these options.
- **mongodb.uri\_options** (optional): Additional connection string options, which will overwrite any options with the same name in the `uri` or `connection` parameter.
- **mongodb.driver\_options** (optional): An array of options for the MongoDB driver.

The `uri` parameter overrides `connection`.

Services
--------

[](#services)

- **mongodb**: The [`MongoDB\Client`](https://docs.mongodb.com/php-library/current/reference/class/MongoDBClient) connection instance. The main way of interacting with MongoDB.
- **mongodb.clients**: The collection of MongoDB client instances. See section on [using multiple clients](#using-multiple-clients) for details.
- **mongodb.client**: Factory for `MongoDB\Client` connection instances.

Registering
-----------

[](#registering)

**Example #1 Connecting to a replica set named *test***

```
$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.uri' => "mongodb://db1.example.net:27017,db2.example.net:2500/?replicaSet=test",
));

// or

$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.connection' => "db1.example.net:27017,db2.example.net:2500/?replicaSet=test",
));

// or

$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.connection' => array(
        'hosts' => array(
            "db1.example.net:27017",
            array(
                'host' => "db2.example.net",
                'port' => 2500
            ),
        ),
        'options' => array(
            'replicaSet' => "test",
        )
    ),
));
```

All the registered connections above are equivalent.

**Example #2 Connecting to a sharded cluster**

```
$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.connection' => "r1.example.net:27017,r2.example.net:27017",
));

// or

$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.connection' => array(
        'hosts' => array(
            array('host' => "r1.example.net", 'port' => 27017),
            array('host' => "r2.example.net", 'port' => 27017),
        ),
    ),
));
```

**Example #3 Connecting to a UNIX domain socket with file path */tmp/mongodb-27017.sock***

```
$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.connection' => rawurlencode("/tmp/mongodb-27017.sock"),
));
```

Add MongoDB as a dependency:

```
composer require liquidbox/silex-mongodb:^2.0
```

Usage
-----

[](#usage)

**Example #1: Inserting a document into the *beers* collection of the *demo* database**

```
$collection = $app['mongodb']->demo->beers;

$result = $collection->insertOne(array(
    'name'    => "Hinterland",
    'brewery' => "BrewDog",
));

echo "Inserted with Object ID '{$result->getInsertedId()}'";
```

**Example #2: Using the find method**

```
$collection = $app['mongodb']->demo->beers;

$results = $collection->find(array(
    'name'    => "Hinterland",
    'brewery' => "BrewDog",
));

foreach ($results as $entry) {
    printf('%d: %s' . PHP_EOL, $entry['_id'], $entry['name']);
}
```

Using multiple clients
----------------------

[](#using-multiple-clients)

The MongoDB provider can allow the use of multiple clients. In order to configure the URIs, use **mongodb.uri** as an array of configurations where keys are connection names and values are parameters:

```
$config['mongodb']['replica_name']    = "test";
$config['mongodb']['replica_cluster'] = array(
    "example1.com",
    "example2.com",
    "example3.com",
);

// ...

$app->register(new LiquidBox\Silex\Provider\MongoDbServiceProvider(), array(
    'mongodb.uri' => array(
        'mongo_read' => array(
            'connection' => array(
                'hosts'   => $config['mongodb']['replica_cluster'],
                'options' => array(
                    'replicaSet'     => $config['mongodb']['replica_name'],
                    'readPreference' => "secondary",
                )
            ),
        ),
        'mongo_write' => array(
            'connection' => array(
                'hosts'   => $config['mongodb']['replica_cluster'],
                'options' => array(
                    'replicaSet' => $config['mongodb']['replica_name'],
                    'w'          => 2
                    'wtimeoutMS' => 2000,
                )
            ),
        ),
    ),
));
```

The first registered connection is the default and can simply be accessed as you would if there was only one connection. Given the above configuration, these two lines are equivalent:

```
$app['mongodb']->zips->find(array('city' => "JERSEY CITY", 'state' => "NJ"));

$app['mongodb.clients']['mongo_read']->zips->find(array('city' => "JERSEY CITY", 'state' => "NJ"));
```

For more information, check out the [official MongoDB documentation](https://docs.mongodb.com/).

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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://avatars.githubusercontent.com/u/17163576?v=4)[liquidBOX](/maintainers/liquidbox)[@liquidbox](https://github.com/liquidbox)

---

Top Contributors

[![ExplicitDev](https://avatars.githubusercontent.com/u/17163543?v=4)](https://github.com/ExplicitDev "ExplicitDev (1 commits)")

### Embed Badge

![Health badge](/badges/liquidbox-silex-mongodb/health.svg)

```
[![Health](https://phpackages.com/badges/liquidbox-silex-mongodb/health.svg)](https://phpackages.com/packages/liquidbox-silex-mongodb)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k104.3M836](/packages/laravel-socialite)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k38.6M289](/packages/laravel-dusk)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

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