Skip to content

Added support for $uuid#20

Open
niccottrell wants to merge 1 commit into
rueckstiess:masterfrom
niccottrell:master
Open

Added support for $uuid#20
niccottrell wants to merge 1 commit into
rueckstiess:masterfrom
niccottrell:master

Conversation

@niccottrell

Copy link
Copy Markdown

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

@rueckstiess

Copy link
Copy Markdown
Owner

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

@niccottrell

Copy link
Copy Markdown
Author

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.

@rueckstiess

Copy link
Copy Markdown
Owner

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"}

@niccottrell

Copy link
Copy Markdown
Author

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.

@mhelmstetter

Copy link
Copy Markdown

mongoimport and extended json v2 now natively support uuid, can we add this support?

@rueckstiess

Copy link
Copy Markdown
Owner

Hi @mhelmstetter, just to confirm how this feature should work exactly:

Does this look right?

mgeneratejs '{my_id: "$uuid"}' -n3 

would produce the following output:

{"my_id": {"$uuid": "68a0f36d-2275-560a-9b71-3fbab319b0ba"}}
{"my_id": {"$uuid": "7a6e43b5-3378-5281-9e05-af9f9100ff4c"}}
{"my_id": {"$uuid": "0bc8eb87-bf01-5205-be07-7df7849d944f"}}

According to https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#type-representations this is now the extended json format v2 for UUIDs, both relaxed and canonical form.

Is that what you'd expect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants