MongoDB best practices docs: MongoDB-record optimizing ObjectIDs

169 views
Skip to first unread message

alexmnyc

unread,
Oct 8, 2012, 4:48:35 PM10/8/12
to lif...@googlegroups.com
Hi guys,

I looked into mongodb-record framework and was not able to find an implementation of the ObjectIdField which would use base64 encoded approach described in the Best Practices docs from MongoDB which is related to "Store Binary GUIDs as BinData, rather than as hex encoded strings"


What is the recommended approach in Lift community to implement such an optimization?

Thank you.

Alex

Tim Nelson

unread,
Oct 9, 2012, 5:59:32 AM10/9/12
to lif...@googlegroups.com
Hi Alex,

These two things are not related. 

"Store Binary GUIDs as BinData, rather than as hex encoded strings" This is what ObjectIdField does. 

The "base64 encoded approach" is for storing the id of "foreign keys" in an embedded document, which will store them as Strings.

Tim

alexmnyc

unread,
Oct 9, 2012, 11:38:31 AM10/9/12
to lif...@googlegroups.com
If I dig into the source of ObjectId.java from org.bson.types.ObjectId, I see that there is an anticipation for the valid id to be a 24 character hexadecimal represented string. 
It looks like ObjectIdField has an implicit dependency on this convention as well. For example, 

this is from package net.liftweb.mongodb.record.field.ObjectIdField:

def setFromString(in: String): Box[ObjectId] =
    if (ObjectId.isValid(in))
      setBox(Full(new ObjectId(in)))
    else
      setBox(Failure("Invalid ObjectId string: "+in))


Which would mean that anything other than a 24 character long hex represented string would not get Box'ed with a value.
Am I looking in the wrong place? I'm totally confused.

This is from org.bson.types.ObjectId:

public static boolean isValid( String s ){
        if ( s == null )
            return false;

        final int len = s.length();
        if ( len != 24 )
            return false;

        for ( int i=0; i<len; i++ ){
            char c = s.charAt( i );
            if ( c >= '0' && c <= '9' )
                continue;
            if ( c >= 'a' && c <= 'f' )
                continue;
            if ( c >= 'A' && c <= 'F' )
                continue;

            return false;
        }

        return true;
    }

Tim Nelson

unread,
Oct 9, 2012, 4:59:20 PM10/9/12
to lif...@googlegroups.com
The isValid function checks whether or not a String representation of an ObjectId is valid or not. It has nothing to do with how ObjectIds are stored in the database.
Reply all
Reply to author
Forward
0 new messages