As the title states, I'm looking for a way to generate a NUUID
(.NET Guid compatible id) in a script for the mongodb shell.
Asked
Active
Viewed 1,891 times
0

david.s
- 11,283
- 6
- 50
- 82
1 Answers
0
Usage:
NUUID(uuid())
The uuid
generator is based on this stackoverflow answer:
function uuid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
The conversion from uuid
to mongodb BinData
is based on uuidhelpers.js:
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;
}
function NUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
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);
}
-
Will you care to explain what is `BinData()` I found nothing like it in javascript – Vinod Srivastav Mar 27 '23 at 15:33
-
1https://www.mongodb.com/docs/manual/reference/method/BinData/ – david.s Mar 29 '23 at 13:54
-
Can you please define `NUUID` I am not able find much info about it, like the algorithm and the format. ? – Vinod Srivastav Apr 10 '23 at 10:40