Created
January 11, 2020 09:23
-
-
Save webdevilopers/a38075d7bb46710bc49ab64f5eeb42f6 to your computer and use it in GitHub Desktop.
How to convert PHP MongoDB BSON Binary to (L)UUID or ObjectId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Came from:
Requires PHP
mongodb
extension - not sure if this will work with themongo
legacy extension.