MongoId object vs. string

14 views
Skip to first unread message

Michael Lugassy

unread,
Nov 25, 2009, 7:59:57 AM11/25/09
to mongod...@googlegroups.com
Guys --

I'm pre-generating document IDs using "new MongoId()" and later use
them in the application (faster than waiting or relaying on the $doc
object to return). Is there a benefit of using the MongoId object vs.
the __toString() hexidecimal representation? which is faster assuming
I later use:

$criteria = array('_id' => $id);
$doc = array('$inc' => array('clicked => 1));
$collection->update($criteria, $doc);

Thanks!

Eliot Horowitz

unread,
Nov 25, 2009, 9:12:31 AM11/25/09
to mongod...@googlegroups.com
Using the MongoID itself is going to be better long term because its
12 bytes rather than 24 that the string needs.
If you have your own unique string that is short and guaranteed to be
unique, then that is a good solution as well.
But _id size can be important so that the index fits in ram.
> --
>
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>
>

Nicolas Clairon

unread,
Nov 25, 2009, 10:41:31 AM11/25/09
to mongod...@googlegroups.com
> Using the MongoID itself is going to be better long term because its
> 12 bytes rather than 24 that the string needs.
> If you have your own unique string that is short and guaranteed to be
> unique, then that is a good solution as well.
> But _id size can be important so that the index fits in ram.

Said that, I suppose that stocking url into the '_id' field is a very
bad idea as
urls could be very long... right ?

Kristina Chodorow

unread,
Nov 25, 2009, 10:48:00 AM11/25/09
to mongod...@googlegroups.com
Depends... If you're going to have to store URLs in the document
anyway, you're saving the 12 OID bytes, plus the field name for the
url.

Mathias Stearn

unread,
Nov 25, 2009, 11:00:19 AM11/25/09
to mongod...@googlegroups.com
More important than the length is that they tend to have long identical prefixes which slows down index traversal. If you can get away with it, storing URLs backwards will speed things up.

On Wed, Nov 25, 2009 at 10:41 AM, Nicolas Clairon <cla...@gmail.com> wrote:

Nicolas Clairon

unread,
Nov 25, 2009, 11:33:32 AM11/25/09
to mongod...@googlegroups.com
So, to prevent index size and speed things up, it is better to do :

{
'_id' : ObjectId(),
'url' : 'http://www.my_really_really_really.com/really/really/long/url'
...
}

with in index (unique) on the field url than :

{
'_id' :'http://www.my_really_really_really.com/really/really/long/url'
...
}

especially if the doc contains the url in another field anyway...

Eliot Horowitz

unread,
Nov 25, 2009, 8:56:57 PM11/25/09
to mongod...@googlegroups.com
No - if you have a unique key - then you should use it.
But in the case of a long url, you might be better of splitting host
from path, and maybe even reversing path for indexing.
Reply all
Reply to author
Forward
0 new messages