Skip to content

DOCSP-41972: GridFS guide #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Sep 16, 2024
Binary file added source/includes/figures/GridFS-upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions source/includes/write/gridfs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
require 'vendor/autoload.php';

use MongoDB\Client;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this earlier. The code below uses new MongoDB\Client(...) so this use statement has no effect. Consider removing it.

Alternatively, decide if you want to rely on use statements for all class references in the interest of consistency. I see you're doing that for new ObjectId(...).

use MongoDB\BSON\ObjectId;

$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
$client = new Client($uri);

// start-to-json
function toJSON(object $document): string
{
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
}
// end-to-json

// Creates a GridFS bucket or references an existing one
// start-create-bucket
$bucket = $client->db->selectGridFSBucket();
// end-create-bucket

// Creates or references a GridFS bucket with a custom name
// start-create-custom-bucket
$custom_bucket = $client->db->selectGridFSBucket(
['bucketName' => 'myCustomBucket']
);
// end-create-custom-bucket

// Uploads a file called "my_file" to the GridFS bucket and writes data to it
// start-open-upload-stream
$stream = $bucket->openUploadStream('my_file', [
'metadata' => ['contentType' => 'text/plain']
]);
fwrite($stream, 'Data to store');
fclose($stream);
// end-open-upload-stream

// Uploads data to a stream, then writes the stream to a GridFS file
// start-upload-from-stream
$file = fopen('/path/to/input_file', 'rb');
$bucket->uploadFromStream('new_file', $file);
// end-upload-from-stream

// Prints information about each file in the bucket
// start-retrieve-file-info
$files = $bucket->find();
foreach ($files as $file_doc) {
echo toJSON($file_doc), PHP_EOL;
}
// end-retrieve-file-info

// Downloads the "my_file" file from the GridFS bucket and prints its contents
// start-open-download-stream-name
$stream = $bucket->openDownloadStreamByName('my_file');
$contents = stream_get_contents($stream);
echo $contents, PHP_EOL;
fclose($stream);
// end-open-download-stream-name

// Downloads a file from the GridFS bucket by referencing its ObjectId value
// start-download-files-id
$stream = $bucket->openDownloadStream(new ObjectId('66e0a5487c880f844c0a32b1'));
$contents = stream_get_contents($stream);
fclose($stream);
// end-download-files-id

// Downloads the original "my_file" file from the GridFS bucket
// start-download-file-revision
$stream = $bucket->openDownloadStreamByName('my_file', ['revision' => 0]);
$contents = stream_get_contents($stream);
fclose($stream);
// end-download-file-revision

// Downloads an entire GridFS file to a download stream
// start-download-to-stream
$file = fopen('/path/to/output_file', 'wb');
$bucket->downloadToStream(
new ObjectId('66e0a5487c880f844c0a32b1'),
$file,
);
// end-download-to-stream

// Renames a file from the GridFS bucket with the specified ObjectId
// start-rename-files
$bucket->rename(new ObjectId('66e0a5487c880f844c0a32b1'), 'new_file_name');
// end-rename-files

// Deletes a file from the GridFS bucket with the specified ObjectId
// start-delete-files
$bucket->delete(new ObjectId('66e0a5487c880f844c0a32b1'));
// end-delete-files
1 change: 1 addition & 0 deletions source/write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Write Data to MongoDB
/write/replace
/write/insert
/write/update
/write/gridfs

Overview
--------
Expand Down
Loading
Loading