dev-master
9999999-devA MongoDB service provider for the Silex micro-framework
MIT
The Requires
The Development Requires
by Jonathan-Paul Marois
mongodb database service php silex mongo provider
A MongoDB service provider for the Silex micro-framework
You are reading the documentation for Silex 2.x. Switch to the documentation for Silex 1.x., (*2)
The MongoDbServiceProvider provides integration with the MongoDB extension., (*3)
uri
or connection
parameter.The uri
parameter overrides connection
., (*4)
MongoDB\Client
connection instance. The main way of interacting with MongoDB.MongoDB\Client
connection instances.Example #1 Connecting to a replica set named test, (*5)
$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., (*6)
Example #2 Connecting to a sharded cluster, (*7)
$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, (*8)
$app->register(new \LiquidBox\Silex\Provider\MongoDbServiceProvider(), array( 'mongodb.connection' => rawurlencode("/tmp/mongodb-27017.sock"), ));
Add MongoDB as a dependency:, (*9)
composer require liquidbox/silex-mongodb:^2.0
Example #1: Inserting a document into the beers collection of the demo database, (*10)
$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, (*11)
$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']); }
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:, (*12)
$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:, (*13)
$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., (*14)
A MongoDB service provider for the Silex micro-framework
MIT
mongodb database service php silex mongo provider