Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Selecting (max) OR identity field
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Kennedy  
View profile  
 More options Jun 9 2010, 2:42 pm
From: Michael Kennedy <mkennedy66996...@gmail.com>
Date: Wed, 9 Jun 2010 11:42:02 -0700
Local: Wed, Jun 9 2010 2:42 pm
Subject: Re: [NoRM MongoDB] Re: Selecting (max) OR identity field

I've been working on a project which has the exact same requirements. Here's
how I solved it.

I have a proper internal ID using ObjectId, but for referencing from URLs
and whatnot I created as second property called PublicId. Then I just
generate a random 6 character alphanumeric string and try to get it from the
database. If one exists, then I'll just regen that id, otherwise I insert
the entry with that ID. Chances of a conflict / race condition are utterly
small.

The actual code is below.

Best regards,
Michael

Note: UniqunessMethod is a delegate I pass in which basically does a select
from mongodb where publicid == random key. But I use a delegate so I can run
this for any entity I want.

private string GetNewPublicId<TEntity>(Func<string, TEntity>
UniqunessMethod)
where TEntity : class
{
Random rand = new Random();
bool unique = false;
string id = null;

while (!unique)
{
StringBuilder sb = new StringBuilder();
 for (int i = 0; i < 6; i++)
{
int next = rand.Next(0, 36);
 if (next < 10)
{
sb.Append(next);
 }
else
{
 next -= 10;
next = 'a' + next;
sb.Append((char) next);
 }

}

id = sb.ToString();
 TEntity entity = UniqunessMethod(id);
unique = entity == null;

}

return id;
}
On Tue, Jun 8, 2010 at 8:50 AM, SaraC <sarajchi...@gmail.com> wrote:
> The problem is this application is a url shortener and requires an
> identifier that can be hashed and
> used for those purposes. I don't know of another solution while
> ensuring uniqueness.

> On Jun 8, 2:49 am, Ken Egozi <egoz...@gmail.com> wrote:
> > select MAX is definitely wrong for the reasons mentioned.
> > I would avoid Identity behaviour anyway, and not only because of the
> > sharding issue. Using Identity means that every Insert must immediately
> call
> > the database. This is not always desirable, as you might want to do more
> > stuff with the Id, but postpone the DB call for later for various
> reasons.

> > Now using the ObjectId approach has its limitations also, mainly the size
> > and the readability, as some systems like human readable ids (order
> number,
> > user id, customer service call id, etc.)
> > you can enjoy both worlds if you employ the hi-lo algorithm (or
> > a variation thereof). You get an integer id, generally sequential, fast
> to
> > generate and simple to implement.

> > The docs for Hibernate/NHibernate are all full with reasons to avoid
> > Identity and use GuidComb (their equivalence of ObjectId) or HiLo

> > On Tue, Jun 8, 2010 at 9:35 AM, Jørn Wildt <j...@fjeldgruppen.dk> wrote:
> > > Check this request for some thoughts:
> > >http://jira.mongodb.org/browse/SERVER-195

> > > Beware of concurrency issues: two reades may read MAX() = 10 and then
> > > both of them increment it to 11. You need to use MongoDB's various
> > > atomic operation to ensure this operation is safe.

> > > /Jørn

> > > On Jun 8, 8:19 am, SaraC <sarajchi...@gmail.com> wrote:
> > > > I'm new to NoSQL, I'm looking to either create an identity field
> (that
> > > > starts at 0) or build my own by selecting a max value.

> > > > I understand that the Mongo Ids are timestamps and can be used as
> > > > such. However, I need a smaller value for this column.

> > > > Thanks a bunch!

> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "NoRM mongodb" group.
> > > To post to this group, send email to norm-mongodb@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > norm-mongodb+unsubscribe@googlegroups.com<norm-mongodb%2Bunsubscribe@google groups.com>
> <norm-mongodb%2Bunsubscribe@google groups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/norm-mongodb?hl=en.

> > --
> > Ken Egozi.
> http://www.kenegozi.com/bloghttp://www.delver.comhttp://www.musicglue...כנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

> --
> You received this message because you are subscribed to the Google Groups
> "NoRM mongodb" group.
> To post to this group, send email to norm-mongodb@googlegroups.com.
> To unsubscribe from this group, send email to
> norm-mongodb+unsubscribe@googlegroups.com<norm-mongodb%2Bunsubscribe@google groups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/norm-mongodb?hl=en.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.