$luuid ='e4eaaaf2-d142-11e1-b3e4-080027620cdd'; //格式一定要正确 » RFC 4122.

$uuid='0123456789abcdef';
$id = new \MongoDB\BSON\Binary($uuid,\MongoDB\BSON\Binary::TYPE_UUID);

查mongodb的LUUID类型为以下代码

$luuid= str_replace("-","",$luuid);
$id= new \MongoDB\BSON\Binary(hex2bin($luuid),\MongoDB\BSON\Binary::TYPE_OLD_UUID);
$id=bin2hex($id);
var_dump($id);
luuid 转为uuid:
$luuid=二进制字符
$id=bin2hex($luuid);
$a = substr($id,6, 2).substr($id,4, 2).substr($id,2, 2).substr($id,0, 2);
$b = substr($id,10, 2).substr($id,8, 2);
$c = substr($id,14, 2).substr($id,12, 2);
$d = substr($id,16, 16);
$id = $a.$b.$c.$d;
$id = substr($id,0, 8).'-'.substr($id,8, 4).'-'.substr($id,12, 4).'-'.substr($id,16, 4).'-'.substr($id,20, 12);
var_dump($id);
uuid与luuid互转
publicfunctionreplayLuuid($id,$type)
{
$id = str_replace("-","",$id);
$a = substr($id,6, 2).substr($id,4, 2).substr($id,2, 2).substr($id,0, 2);
$b = substr($id,10, 2).substr($id,8, 2);
$c = substr($id,14, 2).substr($id,12, 2);
$d = substr($id,16, 16);
$id = $a.$b.$c.$d;
if ($type =='to') {
$id= new \MongoDB\BSON\Binary(hex2bin($id),\MongoDB\BSON\Binary::TYPE_OLD_UUID);
}else{
$id = substr($id,0, 8).'-'.substr($id,8, 4).'-'.substr($id,12, 4).'-'.substr($id,16, 4).'-'.substr($id,20, 12);
}
return $id;
}

最后更新于 2021年2月5日

php 插入mongodb uuid类型
标签: