Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Created January 11, 2020 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/a38075d7bb46710bc49ab64f5eeb42f6 to your computer and use it in GitHub Desktop.
Save webdevilopers/a38075d7bb46710bc49ab64f5eeb42f6 to your computer and use it in GitHub Desktop.
How to convert PHP MongoDB BSON Binary to (L)UUID or ObjectId
<?php
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId;
function mongoBinaryToUuid(Binary $bin): string
{
$hex = bin2hex($bin->getData());
return substr($hex, 0, 32);
}
function mongoBinaryToObjectId(Binary $bin): ObjectId
{
$hex = bin2hex($bin->getData());
return new ObjectId(substr($hex, 0, 24));
}
@webdevilopers
Copy link
Author

Came from:

Requires PHP mongodb extension - not sure if this will work with the mongo legacy extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment