How to make unique username/user id/object?

130 views
Skip to first unread message

Joni Sebastian

unread,
Apr 22, 2013, 4:03:59 PM4/22/13
to mgo-...@googlegroups.com
Maybe this question has been asked a lot, but i haven't found any post like that. Afaik, the solution to this particular problem with MongoDB depends on the language used.
What i'm trying to do was to make something like youtube, you know, with unique username and unique video id. How to do that with mgo?

Ingo Gottwald

unread,
Apr 23, 2013, 6:43:19 AM4/23/13
to mgo-...@googlegroups.com
Hi,

i don't think that this is language or driver specific question.

If you need something like uniqe IDs then mognodb offers its ObjectID that you can use - regardless of the driver.

As username that would be kind of hard to remember, so you could either use a unique index with some user-defined usernames which will give you annoying usernames like mine-is-already-taken345.
Or you choose the registrants e-mail address as username, which will always be unique when you verify it with a opt-in mail at registration.

But nothing of that is driver or language specific.

Regards

Ingo

Joni Sebastian

unread,
Apr 23, 2013, 8:42:42 AM4/23/13
to mgo-...@googlegroups.com

I heard the ObjectID is not that reliable as it will be a problem when you do the sharding or something.

Ingo Gottwald

unread,
Apr 23, 2013, 9:12:19 AM4/23/13
to mgo-...@googlegroups.com
Well the manual (http://docs.mongodb.org/manual/reference/object-id/) tells you something different:

ObjectId is a 12-byte BSON type, constructed using:

a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.

So with sharding you are on different machines and therefore you have different machine identifiers.

The warning in the reference is about time based ordering.
You should use other fields for that, like a timestamp.
Message has been deleted

Joni Sebastian

unread,
Apr 23, 2013, 7:26:52 PM4/23/13
to mgo-...@googlegroups.com
Thank you for the fast reply. I will try what you've advised.
But is there any other way?

Ingo Gottwald

unread,
Apr 24, 2013, 2:53:38 AM4/24/13
to mgo-...@googlegroups.com
There are always a lot of ways how to get things done.
Try to invent one. That's the joy of programing. :-)


2013/4/24 Joni Sebastian <pembala...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "mgo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mgo-users/eLeUQ2qJYiw/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jim Robinson

unread,
May 20, 2013, 8:25:58 AM5/20/13
to mgo-...@googlegroups.com
What I had read was that sharding based on the ObjectId was
ok for mixed read/write operations, but was not so good for
applications that mostly created new documents (e.g., say a
log entry database).  The reason being that the time component
of the ObjectId would tend to cause one node in the cluster to
become a hotspot for new documents.
Reply all
Reply to author
Forward
0 new messages