PHP code example of mts88 / mongogrid
1. Go to this page and download the library: Download mts88/mongogrid 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/ */
mts88 / mongogrid example snippets
Mts88\MongoGrid\Providers\MongoGridServiceProvider,
'MongoGrid' => Mts88\MongoGrid\Facades\MongoGrid,
php artisan vendor:publish
'db_config' => [
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database
'db_config' => [
'host' => [
[
'address' => 'server1',
'port'=> 27017
],
[
'address' => 'server2',
'port' => 27017
],
],
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'replicaSet' => env('DB_REPLICA_SET'),
'database' => 'admin' // sets the authentication database
'db_config' => env('DB_CONNECTION', 'mongodb'),
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database
'mongodb' => [
'driver' => 'mongodb',
'host' => ['server1', 'server2'],
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'replicaSet' => 'replicaSetName'
]
],
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_DSN'),
'database' => env('DB_DATABASE'),
],
'bucket' => [
'prefix' => 'fs',
'chunkSizeBytes' => 261120,
'readPreference' => 'primaryPreferred',
'readConcern' => 'available',
],
'add_meta' => false,
'storage' => 'local',
MongoGrid::prefix('myNewPrefix')->someCoolMethod();
$fileName = 'differentName.jpg';
$fileContent = Storage::disk('local')->get('star-wars-logo.jpg');
$objectId = MongoGrid::storeFile($fileContent, $fileName);
// Or with your custom metadata
$fileName = 'differentName.jpg';
$fileContent = Storage::disk('local')->get('star-wars-logo.jpg');
$metadata = array(
'father' => 'Anakin',
'son' => 'Luke'
);
$objectId = MongoGrid::storeFile($fileContent, $fileName, $metadata);
$fileName = 'differentName.jpg';
$fileContent = Storage::disk('local')->get('star-wars-logo.jpg');
$objectId = MongoGrid::prefix('starWars')->storeFile($fileContent, $fileName);
// Or with your custom metadata
$fileName = 'differentName.jpg';
$fileContent = Storage::disk('local')->get('star-wars-logo.jpg');
$metadata = array(
'father' => 'Anakin',
'son' => 'Luke'
);
$objectId = MongoGrid::prefix('starWars')->storeFile($fileContent, $fileName, $metadata);
$fileName = 'star-wars-logo.jpg';
$content = MongoGrid::getFileContent($fileName, '-1');
// Or by ObjectId
$objectId = new \MongoDB\BSON\ObjectId;
$content = MongoGrid::getFileContent($objectId);
$fileName = 'star-wars-logo.jpg';
$document = MongoGrid::getFile($fileName);
// Or by ObjectId
$objectId = new \MongoDB\BSON\ObjectId;
$document = MongoGrid::getFile($objectId);
$objectId = new \MongoDB\BSON\ObjectId;
$document = MongoGrid::findOne([ '_id' => $objectId ]);
$objectId = new \MongoDB\BSON\ObjectId;
$document = MongoGrid::find([ '_id' => $objectId ]);
$objectId = new \MongoDB\BSON\ObjectId;
$path = 'your/awesome/path';
MongoGrid::download($objectId, $path);
//Or by filename
$fileName = 'star-wars-logo.jpg';
$path = 'your/awesome/path';
MongoGrid::download($fileName, $path, '-1');
$objectId = new \MongoDB\BSON\ObjectId;
$newName = 'star-wars-amazing-logo.jpg';
MongoGrid::rename($objectId, $newName);
$objectId = new \MongoDB\BSON\ObjectId;
MongoGrid::delete($objectId);
$bucketName = MongoGrid::getBucketName();
$collection = MongoGrid::getChunksCollection();
$size = MongoGrid::getChunkSizeBytes();
$databaseName = MongoGrid::getDatabaseName();
$collection = MongoGrid::getFilesCollection();
MongoGrid::drop();