mgeneratejs icon indicating copy to clipboard operation
mgeneratejs copied to clipboard

Added support for $uuid

Open niccottrell opened this issue 7 years ago • 4 comments

This seems to work but creates ugly entries like [{"$binary":"YTNhZTU0Y2ItZTczMS00YmEwLTk1ZmYtNDM5N2YxZmI5OWVk","$type":"4"}. Any idea how I can customize the JSON output?

niccottrell avatar Feb 26 '18 19:02 niccottrell

Hi, what output do you want to create? Can you give an example?

rueckstiess avatar Feb 26 '18 22:02 rueckstiess

Hi Thomas. I was hoping for output in the JSON like this:

blah: UUID("176bf052-4ecb-4e1d-b48f-f3523f7f277f")

I'm pushing a customer to store UUID as binary in BSON rather than as a string, since I understand it takes ~ 50% the number of bytes to store.

niccottrell avatar Feb 27 '18 07:02 niccottrell

Hi @niccottrell,

Your example is not valid JSON, you wouldn't be able to parse that with a JSON parser or use mongoimport to write it back into a database.

MongoDB's extended JSON offers a "strict" and "shell" version. You describe the shell version. The strict version (which mgeneratejs and mongoimport/export use) needs to use the {"$binary":"...","$type":"4"} format and subtype 4 is specifically used for UUIDs, see the BSON spec.

If you just want the UUID as a string, you can use the Chance.js $guid operator to generate the UUID string:

mgeneratejs '{uuid: "$guid"}' -n5

{"uuid":"7f93d20e-2776-5630-830d-729996ee2d3b"}
{"uuid":"2ad47201-30e8-5ff2-8a70-cd1d1e02743c"}
{"uuid":"e6cee534-8332-5216-9767-c9a3f286695e"}
{"uuid":"0aa0906b-5972-57d0-9030-cd1e3e0c15b1"}
{"uuid":"34b509d2-582d-5375-b439-1aadd7a9b379"}

rueckstiess avatar Feb 28 '18 05:02 rueckstiess

Hi Thomas! Thanks - I wasn't aware that mongoimport didn't support the UUID("...") format. But it does support ObjectId("123...") in the JSON to import. Thanks for the $guid tip but I really wanted to create example JSON which, once imported, would be stored as the BSON UUID binary type since I'm looking at getting the filesize per document down as much as possible, but still have the mgenerated documents human-readable as UUID.

niccottrell avatar Feb 28 '18 09:02 niccottrell