Mongo UUID 类型 03 而不是来自 mongo shell 的 04 [英] Mongo UUID Type 03 instead of 04 from mongo shell

查看:49
本文介绍了Mongo UUID 类型 03 而不是来自 mongo shell 的 04的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Mongo Shell 的 MongoDB UUID 转换结果是 TYPE 03,旧格式.我们如何让它与 TYPE 04 二进制格式一起工作?

MongoDB UUID conversion from Mongo Shell turns out be TYPE 03, legacy format. How do we get it to work with TYPE 04, binary format?

> db.foo.insert({"_id":1000,"key1":UUID("240003A09CEC456AB57B98FF8E0E45DB")})
  WriteResult({ "nInserted" : 1 })
> db.foo.find({_id:1000})
  { "_id" : 1000, "key1" : BinData(3,"JAADoJzsRWq1e5j/jg5F2w==") }
> db.version()
  3.4.2

从 Java 代码中,我们能够使用字节缓冲区并作为类型 04 的二进制 uuid 插入,示例:BinData(4,"XWCwWqIVTfiEkTx9Yl+2UQ==").

From Java code we are able to use bytebuffers and insert as type 04 binary uuid, sample : BinData(4,"XWCwWqIVTfiEkTx9Yl+2UQ==").

推荐答案

您可以将数据作为类型 4 直接放入 BinData() 函数中,方法是提取 base64 编码并提供它:

You can put the data directly into the BinData() function as type 4, by extracting the base64 encoding and supplying it:

var t = BinData(4, UUID("240003a09cec456ab57b98ff8e0e45db").base64())
BinData(4,"JAADoJzsRWq1e5j/jg5F2w==")

然后你仍然从十六进制得到提供的值:

And then you still get the supplied value from hex:

t.hex()
240003a09cec456ab57b98ff8e0e45db

或者直接提供给HexData():

HexData(4,"240003a09cec456ab57b98ff8e0e45db")
BinData(4,"JAADoJzsRWq1e5j/jg5F2w==")

如果您的数据中间有连字符,请使用 .split().join() 重新格式化:

If you have data with hyphens in between then use .split() and .join() to reformat:

var str =  "240003A0-9CEC-456A-b57B-98FF8e0E45DB";
str.split('-').join("").toLowerCase();
"240003a09cec456ab57b98ff8e0e45db"

存在 JIRA 票证以使 UUID() 默认生成类型 4 SERVER-12835,但这是一个低优先级的问题.对于一般用途,您的交互应该基于驱动程序".但是有这些方法可以使用 shell.

A JIRA ticket exists to make UUID() generate type 4 as default SERVER-12835, but it is a low priority issue. For general usage your interactions should be "driver" based instead. But there are these methods to work with the shell.

这篇关于Mongo UUID 类型 03 而不是来自 mongo shell 的 04的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆