help with UUID in mongoose

570 views
Skip to first unread message

Raif Harik

unread,
Nov 10, 2014, 9:33:33 PM11/10/14
to mongoo...@googlegroups.com

I have the _id in mongo set to a uuid via node plug in "node-uuid" when I look at the value in robomongo it looks like this

"_id" : UUID("5fe6075a-8157-4dfc-8aab-3316ea9cbf17"),

however when I use mongoose to retrieve the value and send it to the client I get a lot of strange characters.

"id":"�\u0007Z�WM���3\u0016ꜿ\u0017"

the schema I'm using is

_id: {
    type: String,
    default: '',
    trim: true
},

is there anyway to make this work?

Thanks,

R

Leonardo S

unread,
Nov 11, 2014, 11:01:43 AM11/11/14
to mongoo...@googlegroups.com
If you want to generate a valid random object id, you can use this function:

var crypto = require('crypto');

function randomObjectId() {
return crypto.createHash('md5').update(Math.random().toString()).digest('hex').substring(0,24);
}

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Valeri Karpov

unread,
Dec 1, 2014, 5:59:42 PM12/1/14
to mongoo...@googlegroups.com
AFAIK uuids are binary data, so not much surprise that the string version looks weird without a wrapper. Looks like node-uuid has wrappers that can take in binary data and output a user-friendly string - perhaps you should use those before passing the uuid up to the client?
Reply all
Reply to author
Forward
0 new messages