doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

Custom _id for GridFS documents #2487

Closed andrey-tech closed 1 year ago

andrey-tech commented 1 year ago

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 commented 1 year ago

@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?

djkoza commented 1 year ago

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 commented 1 year ago

@malarzm Yes, https://github.com/doctrine/mongodb-odm/pull/2489

alcaeus commented 1 year ago

2489 was merged, closing here.