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

Specify _id with uuid #1026

Closed
porsager opened this issue Jul 25, 2012 · 7 comments
Closed

Specify _id with uuid #1026

porsager opened this issue Jul 25, 2012 · 7 comments

Comments

@porsager
Copy link

Hi.

We want to use client generated id's in the form of UUID's in our setup, and would like to know if this is supported by Mongoose, or if there is an implementation to make it work??

It should be possible with Mongo to do this as per: http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-UUIDs

It seems the problem is with casting the _id when creating a new document, so maybe a plugin to mongoose to override this function could be made?

@aheckmann
Copy link
Collaborator

in your schema, declare your _id to be either String or Buffer, then add a function default to generate it for you:

new Schema({ _id: { type: String, default: function genUUID () { .. }}})

@aheckmann
Copy link
Collaborator

the fn should return the value you want to use.

@porsager
Copy link
Author

Ah, right in front of me.

Sorry, i should have been able to figure this out myself. Thank you very much!

@aheckmann
Copy link
Collaborator

Anytime :)

On Jul 25, 2012, at 8:31 AM, porsagerreply@reply.github.com wrote:

Ah, right in front of me.

Sorry, i should have been able to figure this out myself. Thank you very much!


Reply to this email directly or view it on GitHub:
#1026 (comment)

@jaufgang
Copy link

jaufgang commented Nov 7, 2014

in http://docs.mongodb.org/manual/core/document/#the-id-field it says:

For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type.

The Mongo shell has a UUID() method that will convert a UUID string and convert it to BinData format (http://docs.mongodb.org/manual/reference/method/UUID/), but how do I do this through the Mongoose API? Is it possible to read/write the UUID from/to the model as a string but have Mongoose map to the BinData format in the DB? Or is there a convenience method for doing the conversion?

Thanks for any help

@vkarpov15
Copy link
Collaborator

I'm not terribly familiar with UUID's - maybe the Binary type in the node driver will help? See docs

@rsp
Copy link

rsp commented Apr 24, 2017

@jaufgang the UUID() function in Mongo is a little bit strange. It doesn't work for valid UUIDs:

> UUID('343881b7-852c-46cf-b74c-4a0bca2fae9c')
E QUERY    Error: UUID string must have 32 characters

while according to Wikipedia:

In its canonical textual representation, the sixteen octets of a UUID are represented as 32 lowercase hexadecimal (base 16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters

See: https://en.wikipedia.org/wiki/Universally_unique_identifier#Format

It only works when you manually remove the dashes:

> UUID('343881b7852c46cfb74c4a0bca2fae9c')
BinData(3,"NDiBt4UsRs+3TEoLyi+unA==")

@porsager For using UUIDs with Mongoose, there are two modules that you can use:

See also those issues:

and this article for some potential problems with binary UUID representations in Mongo:

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

5 participants