Skip to content

DOCSP-45100: Delete and rename by name #187

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 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions source/includes/write/gridfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,21 @@ function toJSON(object $document): string
// end-download-to-stream

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

// Renames files from the GridFS bucket with the specified filename
// start-rename-files-name
$bucket->renameByName('my_file', 'new_file_name');
// end-rename-files-name

// Deletes a file from the GridFS bucket with the specified ObjectId
// start-delete-files
// start-delete-files-id
$bucket->delete(new ObjectId('66e0a5487c880f844c0a32b1'));
// end-delete-files
// end-delete-files-id

// Deletes a file and its revisions from the GridFS bucket by name
// start-delete-files-name
$bucket->deleteByName('my_file');
// end-delete-files-name
53 changes: 35 additions & 18 deletions source/write/gridfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -375,24 +375,36 @@ Rename Files
------------

Use the ``MongoDB\GridFS\Bucket::rename()`` method to update the name of
a GridFS file in your bucket. You must specify the file to rename by its
``_id`` field rather than its file name.
a GridFS file in your bucket. Pass the following parameters to the ``rename()``
method:

- ``_id`` value of the file you want to rename
- New file name

The following example shows how to update the ``filename`` field to
``'new_file_name'`` by referencing a document's ``_id`` field:
``'new_file_name'`` by referencing a file's ``_id`` value:

.. literalinclude:: /includes/write/gridfs.php
:language: php
:dedent:
:start-after: start-rename-files
:end-before: end-rename-files
:start-after: start-rename-files-id
:end-before: end-rename-files-id

Alternatively, you can use the ``MongoDB\GridFS\Bucket::renameByName()``
method to rename a GridFS file and all its file revisions. Pass the
following parameters to the ``renameByName()`` method:

.. note:: File Revisions
- ``filename`` value you want to change
- New file name

The ``rename()`` method supports updating the name of only one file at
a time. If you want to rename each file revision, or files with different upload
times that share the same file name, collect the ``_id`` values of each revision.
Then, pass each value in separate calls to the ``rename()`` method.
The following example renames all files that have a ``filename`` value
of ``'my_file'``:

.. literalinclude:: /includes/write/gridfs.php
:language: php
:dedent:
:start-after: start-rename-files-name
:end-before: end-rename-files-name

.. _gridfs-delete-files:

Expand All @@ -401,22 +413,27 @@ Delete Files

Use the ``MongoDB\GridFS\Bucket::delete()`` method to remove a file's collection
document and associated chunks from your bucket. This effectively deletes the file.
You must specify the file by its ``_id`` field rather than its file name.
Pass the ``_id`` value of the file you want to delete as a parameter to the
``delete()`` method.

The following example shows you how to delete a file by referencing its ``_id`` field:

.. literalinclude:: /includes/write/gridfs.php
:language: php
:dedent:
:start-after: start-delete-files
:end-before: end-delete-files
:start-after: start-delete-files-id
:end-before: end-delete-files-id

.. note:: File Revisions
Alternatively, you can use the ``MongoDB\GridFS\Bucket::deleteByName()`` method
to delete a file and all its file revisions. Pass the ``filename`` value of the
Copy link
Member

Choose a reason for hiding this comment

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

Text above said "rename a GridFS file and all...", but you omitted "GridFS" here. Should both of these sentences be consistent? I don't have any preference between them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added GridFS!

file you want to delete as a parameter to the ``deleteByName()`` method, as shown
in the following code:

The ``delete()`` method supports deleting only one file at a time. If
you want to delete each file revision, or files with different upload
times that share the same file name, collect the ``_id`` values of each revision.
Then, pass each value in separate calls to the ``delete()`` method.
.. literalinclude:: /includes/write/gridfs.php
:language: php
:dedent:
:start-after: start-delete-files-name
:end-before: end-delete-files-name

API Documentation
-----------------
Expand Down
Loading