MongoDB base64-encoded ObjectId

1,530 views
Skip to first unread message

alexmnyc

unread,
Oct 4, 2012, 1:39:51 PM10/4/12
to lif...@googlegroups.com
Hi guys,

I'm looking at some best practices for schema design for my application and need to be able to implement _id field as Base64-encoded ObjectID

How do you achieve Base64 encoding of ObjectID in Lift? 

Thank you.

alexmnyc

unread,
Oct 4, 2012, 4:46:39 PM10/4/12
to lif...@googlegroups.com
FYI, I'm basing this on the schema provided by Mongo DB use cases document https://jira.mongodb.org/secure/attachment/16610/MongoDB-use-cases.pdf on page 17 collection social.user as example.

Tim Nelson

unread,
Oct 4, 2012, 5:45:38 PM10/4/12
to lif...@googlegroups.com
Hi,

This should do the trick:

import net.liftweb.util.Helpers.base64Encode

val newId = base64Encode(ObjectId.get.toByteArray)

Tim

alexmnyc

unread,
Oct 4, 2012, 5:55:03 PM10/4/12
to lif...@googlegroups.com
Thanks, Tim. It's much appreciated. 

As a matter of fact, I'm trying to make this modification to your greatly contributed mongoauth project for User model. What would be a recommended place to override such logic? How would you do it? Thank you in advance.

Alex Mikhailau

unread,
Oct 5, 2012, 1:22:21 AM10/5/12
to lif...@googlegroups.com
Just to add,

Basically, referencing MongoDB documentation on "Optimizing ObjectIDs", the suggestion is to "store Binary GUIDs as BinData, rather than as hex encoded strings. BSON includes a binary data datatype for storing byte arrays. Using this will make the id values, and their respective keys in the _id index, twice as small."

I was not able to see a BinData data type in the record.field package. How would you switch out mongoauth's User's id to be of BinData data type instead and use base64 encoded values?

Thank you.

Tim Nelson

unread,
Oct 5, 2012, 6:35:47 AM10/5/12
to lif...@googlegroups.com
Hi,

I haven't read the "Optimizing ObjectIDs" docs yet. Give me some time to look into it and I'll get back to you.

Tim

alexmnyc

unread,
Oct 5, 2012, 11:55:28 AM10/5/12
to lif...@googlegroups.com
Thanks, Tim.  

alexmnyc

unread,
Oct 8, 2012, 4:42:39 PM10/8/12
to lif...@googlegroups.com
Hi Tim, were you able to review the documentation? Here's the direct link in case you were not able to find it http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs

PS I'm also going to ask a question in the main thread about what the best approach using Record framework would be to follow MongoDB best practices.

alexmnyc

unread,
Oct 8, 2012, 5:07:46 PM10/8/12
to lif...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages