Search code examples
phpmongodbdoctrinedoctrine-odm

Doctrine MongoDB ODM: how to get the bin_md5 version of the @Id


With Doctrine ODM (MongoDB) it is possible to use the annotation Doctrine\ODM\MongoDB\Mapping\Annotations\Id to set the ID of a document.

As I want to use an hash as ID, I've set my document this way:

class WebResource
{
    /**
     * @ODM\Id(strategy="NONE", type="bin_md5")
     *
     * @var string
     */
    private $hash;
    ...
}

This configuration transforms an hash like 774a0f33ede410cde2d785e2d9e52561 to the _id: 'Nzc0YTBmMzNlZGU0MTBjZGUyZDc4NWUyZDllNTI1NjE='.

Now, when I get the $hash from the document, I get the value 774a0f33ede410cde2d785e2d9e52561.

This makes impossible for me to use a query like this in MongoDB Compass:

{"_id": "774a0f33ede410cde2d785e2d9e52561"}

This query, in fact, will always return no value as the actual ID is Nzc0YTBmMzNlZGU0MTBjZGUyZDc4NWUyZDllNTI1NjE=.

How can I find a document by its ID, having only its hash not still transformed?

I've tried a lot of approaches:

  1. Using md5() php function;
  2. Using (new Binary($resource->getHash(), Binary::TYPE_MD5))->getData()
  3. Using new \MongoBinData($resource->getHash(), \MongoBinData::MD5)

Nothing of those tries seems to work.

Any ideas?


Solution

  • Nzc0YTBmMzNlZGU0MTBjZGUyZDc4NWUyZDllNTI1NjE= is just base64 encoded 774a0f33ede410cde2d785e2d9e52561

    echo -n '774a0f33ede410cde2d785e2d9e52561' | base64
    

    and use the result in compas