从PHP查询Mongo UUID

时间:2017-03-30 17:57:17

标签: php mongodb

我将uuid存储在MongoDB中:

"transactionId" : BinData(3,"/Utm5RS0C5N2/Cwus1rrkw==")

但是当我试图得到这个" transactionId"从PHP,我没有运气,这个查询没有结果:

$query = array("transactionId" => new MongoBinData("/Utm5RS0C5N2/Cwus1rrkw==", MongoBinData::UUID));
$array = $mongo_collection->findOne($query);
var_dump($array);

我怎样才能得到这个" transactionId"来自php?

我自己找到PS解决方案。我们可以找到" transactionId"的十六进制值。在Mongo Shell这样:

var doc = db['ColletctionName'].findOne({"transactionId" : new BinData (3, "/Utm5RS0C5N2/Cwus1rrkw==")})
doc.transactionId.hex()
fd4b66e514b40b9376fc2c2eb35aeb93 

我们看到

的十六进制值
"transactionId" : BinData(3,"/Utm5RS0C5N2/Cwus1rrkw==")

fd4b66e514b40b9376fc2c2eb35aeb93

因此我们可以像这样向Mongo查询以获得" transactionId"领域:

$hex = str_replace("-", "", $transaction_id); // remove extra characters
    $msb = substr($hex, 0, 16);
    $lsb = substr($hex, 16, 16);
    $msb = substr($msb, 14, 2).substr($msb, 12, 2).substr($msb, 10, 2).substr($msb, 8, 2).substr($msb, 6, 2).substr($msb, 4, 2).substr($msb, 2, 2).substr($msb, 0, 2);
    $lsb = substr($lsb, 14, 2).substr($lsb, 12, 2).substr($lsb, 10, 2).substr($lsb, 8, 2).substr($lsb, 6, 2).substr($lsb, 4, 2).substr($lsb, 2, 2).substr($lsb, 0, 2);

    $hex = $msb.''.$lsb;
    $hex = pack('H*',$hex); 
$query = array("transactionId" => new MongoBinData ($hex , MongoBinData::UUID));
$array = $mongo_collection->findOne($query);

0 个答案:

没有答案