3

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

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

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
dilsingi
  • 2,938
  • 14
  • 24

1 Answers1

7

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

Or just supply directly to HexData():

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

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"

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Thanks for the help. Just curiosity, any reason why UUID() function is still kept as old legacy format? Can an option type be added to say type 04. – dilsingi Jul 01 '17 at 23:54
  • @dilsingi It's always been that way, I somehow do not seen the shell changing. There probably is a JIRA ticket around, but as a "utility" it would not be a high priority. As noted, there is `HexData` from the shell as well as the demonstrated method to move data between different types. Main support is rather given to "driver" implementations, which for any industrial purpose, you should be using instead. – Neil Lunn Jul 02 '17 at 00:03
  • @dilsingi Added the JIRA Issue for reference. But as stated, it's low priority. Use the methods as shown when working from the shell. – Neil Lunn Jul 02 '17 at 00:08
  • Thank you once again. Noticed there were some data in the documents with "-" separator. They ended up in error when using HexData db.foo.insert({"_id":1002,"key1":HexData(4,'AC1AAB22-0BF9-4127-BDC2-9C11398130AE')}) 2017-07-01T19:10:41.286-0500 E QUERY [thread1] Error: Invalid hex character in string : @(shell):1:34. Apologized, missed to notice them in my original script. Any tricks to convert them? – dilsingi Jul 02 '17 at 00:15
  • @dilsingi Even with the hyphens removed [that is not a valid value](http://guid.us/Test/GUID). You can strip hyphens with `.split()` and `.join()` JavaScript functions. But the data does need to be valid. That is not. If you have a new question the [Ask a new Question](https://stackoverflow.com/questions/ask) – Neil Lunn Jul 02 '17 at 00:38