MongoDB生成UUID字符串
转换String类型UUID
官方UUID介绍;https://docs.mongodb.com/manual/reference/method/UUID/index.html
Mongodo的UUID默认是返回一个Bson的UUID对象,并非字符串。且UUID需要传递一个(指定一个32字节的十六进制字符串转换为UUID BSON子类型)参数
示例如下
var myuuid = '0123456789abcdeffedcba9876543210' UUID(myuuid)
输出
BinData(4,"ASNFZ4mrze/+3LqYdlQyEA==")
如果想要实现类似于Java生成的UUID那样,需要自定义一个函数方法,借助默认UUID和ObjextID来生成同样的效果。
解决方案如下
参考文章:https://github.com/Bogdan0x400/mongo-csharp-driver/blob/master/uuidhelpers.js
参考文章:https://stackoverflow.com/questions/8244110/get-bindata-uuid-from-mongo-as-string
// 生成常用的UUID,Mongo默认的UUID方法不满足常规需求 function ToGUID(hex) { var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2); var b = hex.substr(10, 2) + hex.substr(8, 2); var c = hex.substr(14, 2) + hex.substr(12, 2); var d = hex.substr(16, 16); hex = a + b + c + d; var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return uuid; } var id = new BinData(3, ObjectId().str); print("UUID:", ToGUID(id.hex()));
输出:UUID:6b9ed6e7-b877-c76f-79d3-7efd75f77ceb
uuidhelpers.js源码
// 用于在MongoDB shell中解析和显示UUID的Javascript助手功能。 // 这是一个临时解决方案,直到SERVER-3153被实现。 // 要创建对应于各种驱动程序编码的BinData值,请使用: // var s = "{00112233-4455-6677-8899-aabbccddeeff}"; // var uuid = UUID(s); // new Standard encoding // var juuid = JUUID(s); // JavaLegacy encoding // var csuuid = CSUUID(s); // CSharpLegacy encoding // var pyuuid = PYUUID(s); // PythonLegacy encoding // 要将各种BinData值转换为可读取的UUID,请使用: // uuid.toUUID() => 'UUID("00112233-4455-6677-8899-aabbccddeeff")' // juuid.ToJUUID() => 'JUUID("00112233-4455-6677-8899-aabbccddeeff")' // csuuid.ToCSUUID() => 'CSUUID("00112233-4455-6677-8899-aabbccddeeff")' // pyuuid.ToPYUUID() => 'PYUUID("00112233-4455-6677-8899-aabbccddeeff")' // 使用任何UUID变体,您可以使用toHexUUID使用子类型和十六进制字符串回显原始BinData: // uuid.toHexUUID() => 'HexData(4, "00112233-4455-6677-8899-aabbccddeeff")' // juuid.toHexUUID() => 'HexData(3, "77665544-3322-1100-ffee-ddccbbaa9988")' // csuuid.toHexUUID() => 'HexData(3, "33221100-5544-7766-8899-aabbccddeeff")' // pyuuid.toHexUUID() => 'HexData(3, "00112233-4455-6677-8899-aabbccddeeff")' /** * 十六进制转换Base64 */ function HexToBase64(hex) { var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var base64 = ""; var group; for (var i = 0; i < 30; i += 6) { group = parseInt(hex.substr(i, 6), 16); base64 += base64Digits[(group >> 18) & 0x3f]; base64 += base64Digits[(group >> 12) & 0x3f]; base64 += base64Digits[(group >> 6) & 0x3f]; base64 += base64Digits[group & 0x3f]; } group = parseInt(hex.substr(30, 2), 16); base64 += base64Digits[(group >> 2) & 0x3f]; base64 += base64Digits[(group << 4) & 0x3f]; base64 += "=="; return base64; } /** * Base64转换十六进制 */ function Base64ToHex(base64) { var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var hexDigits = "0123456789abcdef"; var hex = ""; for (var i = 0; i < 24; ) { var e1 = base64Digits.indexOf(base64[i++]); var e2 = base64Digits.indexOf(base64[i++]); var e3 = base64Digits.indexOf(base64[i++]); var e4 = base64Digits.indexOf(base64[i++]); var c1 = (e1 << 2) | (e2 >> 4); var c2 = ((e2 & 15) << 4) | (e3 >> 2); var c3 = ((e3 & 3) << 6) | e4; hex += hexDigits[c1 >> 4]; hex += hexDigits[c1 & 15]; if (e3 != 64) { hex += hexDigits[c2 >> 4]; hex += hexDigits[c2 & 15]; } if (e4 != 64) { hex += hexDigits[c3 >> 4]; hex += hexDigits[c3 & 15]; } } return hex; } /** * UUID */ function UUID(uuid) { var hex = uuid.replace(/[{}-]/g, ""); //删除多余的字符 var base64 = HexToBase64(hex); return new BinData(4, base64); // new subtype 4 } function JUUID(uuid) { var hex = uuid.replace(/[{}-]/g, ""); //删除多余的字符 var msb = hex.substr(0, 16); var lsb = hex.substr(16, 16); msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2); lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2); hex = msb + lsb; var base64 = HexToBase64(hex); return new BinData(3, base64); } function CSUUID(uuid) { var hex = uuid.replace(/[{}-]/g, ""); //删除多余的字符 var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2); var b = hex.substr(10, 2) + hex.substr(8, 2); var c = hex.substr(14, 2) + hex.substr(12, 2); var d = hex.substr(16, 16); hex = a + b + c + d; var base64 = HexToBase64(hex); return new BinData(3, base64); } function PYUUID(uuid) { var hex = uuid.replace(/[{}-]/g, ""); //删除多余的字符 var base64 = HexToBase64(hex); return new BinData(3, base64); } BinData.prototype.toUUID = function () { var hex = Base64ToHex(this.base64()); //不要使用BinData的hex函数,因为它在旧版本的shell中有错误 var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return 'UUID("' + uuid + '")'; } BinData.prototype.toJUUID = function () { var hex = Base64ToHex(this.base64()); //不要使用BinData的hex函数,因为它在旧版本的shell中有错误 var msb = hex.substr(0, 16); var lsb = hex.substr(16, 16); msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2); lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2); hex = msb + lsb; var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return 'JUUID("' + uuid + '")'; } BinData.prototype.toCSUUID = function () { var hex = Base64ToHex(this.base64()); // 不要使用BinData的十六进制函数,因为它在旧版本的shell中有错误 var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2); var b = hex.substr(10, 2) + hex.substr(8, 2); var c = hex.substr(14, 2) + hex.substr(12, 2); var d = hex.substr(16, 16); hex = a + b + c + d; var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return 'CSUUID("' + uuid + '")'; } BinData.prototype.toPYUUID = function () { var hex = Base64ToHex(this.base64()); // 不要使用BinData的十六进制功能,因为它有错误 var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return 'PYUUID("' + uuid + '")'; } BinData.prototype.toHexUUID = function () { var hex = Base64ToHex(this.base64()); // 不要使用BinData的十六进制功能,因为它有错误 var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12); return 'HexData(' + this.subtype() + ', "' + uuid + '")'; } function TestUUIDHelperFunctions() { var s = "{00112233-4455-6677-8899-aabbccddeeff}"; var uuid = UUID(s); var juuid = JUUID(s); var csuuid = CSUUID(s); var pyuuid = PYUUID(s); print(uuid.toUUID()); print(juuid.toJUUID()); print(csuuid.toCSUUID()); print(pyuuid.toPYUUID()); print(uuid.toHexUUID()); print(juuid.toHexUUID()); print(csuuid.toHexUUID()); print(pyuuid.toHexUUID()); } TestUUIDHelperFunctions()
未经允许请勿转载:程序喵 » MongoDB生成UUID字符串