Skip to content
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

Custom _id for GridFS documents #2487

Closed
andrey-tech opened this issue Dec 11, 2022 · 4 comments
Closed

Custom _id for GridFS documents #2487

andrey-tech opened this issue Dec 11, 2022 · 4 comments

Comments

@andrey-tech
Copy link
Contributor

Feature Request

Q A
New Feature yes
RFC no
BC Break no
Version 2.4.x

Summary

I would like to have a way to set custom file document identifier (_id) for GridFS document, when I insert a new file into GridFS bucket by methods uploadFromFile() or uploadFromStream() of GridFSRepository interface.

This important functionality is required when migrating from MongoDB using old doctrine/mongodb-odm v1.x to a new MongoDB using doctrine/mongodb-odm v2.x. I need to have the same document identifiers (_id) for GridFS documents in old and new MongoDB.

Used MongoDB library (mongodb/mongodb:^1.2.0) supports this functionality in method uploadFromStream() of class Bucket.

I suggest to add the parameter _id as a new property of class UploadOptions and update method prepareOptions() of class DefaultGridFSRepository to support new option _id. So, no big changes are required.

final class UploadOptions
{
    /** @var object|null */
    public $metadata;

    /** @var int|null */
    public $chunkSizeBytes;

    /** @var mixed */
    public $_id;
}
@malarzm
Copy link
Member

malarzm commented Dec 11, 2022

@andrey-tech this sounds like a good idea! While we're tweaking UploadOptions we could also add this missing options

https://github.com/mongodb/mongo-php-library/blob/4c090a88821db2665bf77a0742ae81efec121380/src/GridFS/Bucket.php#L610-L611

@andrey-tech would you be up for a PR?

@malarzm malarzm added this to the 2.5.0 milestone Dec 11, 2022
@djkoza
Copy link

djkoza commented Dec 12, 2022

We need this feature too! A nice improvement would be converting values to a database via mapping type.
With the @andrey-tech tweak, we need to self-convert the UUID object to the Binary object.

$uploadOptions->_id = new \MongoDB\BSON\Binary($uuid->getBytes(), Binary::TYPE_UUID);

with mapping

<id field-name="uuid" strategy="NONE" type="ramsey_uuid_binary" />

@andrey-tech
Copy link
Contributor Author

@malarzm Yes, #2489

@malarzm malarzm added the Has PR label Dec 14, 2022
alcaeus added a commit that referenced this issue Feb 24, 2023
* Fix deprecation notices in CommandLogger (#2485)

* Add _id and disableMD5 to UploadOptions

* Improve UploadOptions

* Set mongodb/mongodb: ^1.4.0

* Rename _id to id in UploadOptions, add two tests

* Change prepareOptions() and test

* Remove disableMD5 from UploadOptions, set mongodb/mongodb ^1.15.0

* Relax mongodb/mongodb requirements

* Relax mongodb/mongodb requirements

* Remove MongoDB 4.2 from testing

---------

Co-authored-by: Ion Bazan <[email protected]>
Co-authored-by: Andreas Braun <[email protected]>
@alcaeus
Copy link
Member

alcaeus commented Feb 27, 2023

#2489 was merged, closing here.

@alcaeus alcaeus closed this as completed Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants