Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for $uuid #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

niccottrell
Copy link

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
Owner

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

@niccottrell
Copy link
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
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
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.

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.

None yet

2 participants