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

Is there a way to make ObjectIds default toString output base64 instead of hex? #184

Closed
fresheneesz opened this issue Jan 22, 2016 · 2 comments

Comments

@fresheneesz
Copy link
Contributor

I'd like to significantly shorten the _ids I pass to clients from my server, but I'm relying on the default toString() implementation in a ton of places. It would be way more convenient for me to be able to change the default encoding so that when I JSON.stringify my documents, ObjectIds automatically output as base 64. Is there a way to do this?

@vkarpov15
Copy link
Contributor

Not that I know of, I don't think mongoskin has any helpers for this. You can always just do doc._id.id.toString('base64');

@fresheneesz
Copy link
Contributor Author

Actually, to anyone who sees this issue, I would instead recommend doing new Buffer(doc._id.toHexString(),'hex').toString('base64') because the result ends up being smaller for some reason. I'm not entirely sure why, but it seems like objectId.id contains more information that needed to represent the id. At least that's what I found in my testing.

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

No branches or pull requests

2 participants