PHP code example of corllete / silex-mongodb-provider
1. Go to this page and download the library: Download corllete/silex-mongodb-provider library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
corllete / silex-mongodb-provider example snippets
use Corllete\SilexMongoDB\Provider\MongoDBServiceProvider;
// connection uri: mongodb://localhost:27017
$app->register(new MongoDBServiceProvider());
/** @var $mongo \MongoDB\Client */
$mongo = $app['mongodb'];
// Instance of MongoDB\Database
$database = $mongo->some_db;
// Instance of MongoDB\Collection
$collection = $mongo->some_collection;
// OR the short version
$collection = $app['mongodb']->some_db->some_collection
// Instance of MongoDB\InsertOneResult
$result = $collection->insertOne([
'name' => 'Gandalf The White'
]);
printf("Inserted %d document(s)\n", $result->getInsertedCount());
// Outputs: Inserted 1 document(s)
// Instance of MongoDB\BSON\ObjectID
$insertedId = $result->getInsertedId();
// Instance of MongoDB\Model\BSONDocument
$document = $collection->findOne(['_id' => $insertedId]);
echo $document['name'];
// 'Gandalf The White'
// Do your ninja magic here!