dongpenggan6812 2017-03-30 17:57
浏览 38

从PHP查询Mongo UUID

I have uuid stored in MongoDB:

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

But when I try to get this "transactionId" from PHP, I have no luck, there are no results for this query:

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

How can I get this "transactionId" from php?

PS Found solution by myself. We can find hex value of "transactionId" in Mongo Shell like this:

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

As we see the hex value of

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

is

fd4b66e514b40b9376fc2c2eb35aeb93

So we can query to Mongo like this to get "transactionId" field:

$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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度