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

Received: by 10.231.169.73 with SMTP id x9mr1102020iby.6.1276108945780;
        Wed, 09 Jun 2010 11:42:25 -0700 (PDT)
X-BeenThere: norm-mongodb@googlegroups.com
Received: by 10.231.174.202 with SMTP id u10ls1145423ibz.2.p; Wed, 09 Jun 2010 
	11:42:25 -0700 (PDT)
Received: by 10.231.167.74 with SMTP id p10mr1104835iby.3.1276108944985;
        Wed, 09 Jun 2010 11:42:24 -0700 (PDT)
Received: by 10.231.167.74 with SMTP id p10mr1104834iby.3.1276108944923;
        Wed, 09 Jun 2010 11:42:24 -0700 (PDT)
Return-Path: <mkennedy66996...@gmail.com>
Received: from mail-iw0-f172.google.com (mail-iw0-f172.google.com [209.85.214.172])
        by gmr-mx.google.com with ESMTP id fa4si5635239ibb.7.2010.06.09.11.42.23;
        Wed, 09 Jun 2010 11:42:23 -0700 (PDT)
Received-SPF: pass (google.com: domain of mkennedy66996...@gmail.com designates 209.85.214.172 as permitted sender) client-ip=209.85.214.172;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of mkennedy66996...@gmail.com designates 209.85.214.172 as permitted sender) smtp.mail=mkennedy66996...@gmail.com; dkim=pass (test mode) header...@gmail.com
Received: by mail-iw0-f172.google.com with SMTP id 42so5665964iwn.3
        for <norm-mongodb@googlegroups.com>; Wed, 09 Jun 2010 11:42:23 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=gamma;
        h=domainkey-signature:received:mime-version:received:in-reply-to
         :references:from:date:message-id:subject:to:cc:content-type;
        bh=b1OTdU4ezZ+qPHvajIM2ieGjfaRE/9wTldY7zKKQxTo=;
        b=SKy6LhlYgPu06u0YmQsrYVko3Q3ubL1ToyZu14eqF3lBMtXj3hM5jrawDrF6kKmWcy
         jQXOK5SbFCXa3i08wnnQfHjCCfkCzA8NDGWAXAMtnJwHGQt2MMOTyD0fkgD9kRPKmV4F
         XlrFW90U3d9nV+eOfmG97L8RcdhhBvkZxDIiY=
DomainKey-Signature: a=rsa-sha1; c=nofws;
        d=gmail.com; s=gamma;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :cc:content-type;
        b=Nm3sLJbACnju84xH4ORkdiq/AVEcKvcZVVwQ5VZChh1Fx8kRJXsinhMC+4RW8yrK/o
         C82zIGaRIuln31porV1JkB04/Ti/o0TCiwnUl2c+l+zwZrkv8Grd6bLgSWOZkWVT5MZg
         i6ey6QnGQ6OBZDASbpHMrsjpaVaLNDYstqj9Y=
Received: by 10.224.63.196 with SMTP id c4mr677441qai.78.1276108943427; Wed, 
	09 Jun 2010 11:42:23 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.220.49.9 with HTTP; Wed, 9 Jun 2010 11:42:02 -0700 (PDT)
In-Reply-To: <8639c78d-def2-4b82-ae2c-61b4c192a...@c33g2000yqm.googlegroups.com>
References: <20bf5912-0708-4f32-a42b-b672e49f8...@i28g2000yqa.googlegroups.com> 
	<9492d739-aa7a-45ce-b519-ab409eb0e...@j4g2000yqh.googlegroups.com> 
	<AANLkTind29vzx83pU5blEJCBz43o8J2kUhlpbpZNt...@mail.gmail.com> 
	<8639c78d-def2-4b82-ae2c-61b4c192a...@c33g2000yqm.googlegroups.com>
From: Michael Kennedy <mkennedy66996...@gmail.com>
Date: Wed, 9 Jun 2010 11:42:02 -0700
Message-ID: <AANLkTikJoKM1EPfLNHZkWL5rSlKgeuLXlnNFXNtuC...@mail.gmail.com>
Subject: Re: [NoRM MongoDB] Re: Selecting (max) OR identity field
To: SaraC <sarajchi...@gmail.com>
Cc: NoRM mongodb <norm-mongodb@googlegroups.com>
Content-Type: multipart/alternative; boundary=0016e64bde324488c904889d4468

--0016e64bde324488c904889d4468
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

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 th=
e
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 =3D=3D random key. But I use a delegate so I ca=
n run
this for any entity I want.

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

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

id =3D sb.ToString();
 TEntity entity =3D UniqunessMethod(id);
unique =3D entity =3D=3D 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 mor=
e
> > 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 si=
ze
> > 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=C3=B8rn 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() =3D 10 and th=
en
> > > both of them increment it to 11. You need to use MongoDB's various
> > > atomic operation to ensure this operation is safe.
> >
> > > /J=C3=B8rn
> >
> > > 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@=
googlegroups.com>
> <norm-mongodb%2Bunsubscribe@google groups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/norm-mongodb?hl=3Den.
> >
> > --
> > Ken Egozi.
> http://www.kenegozi.com/bloghttp://www.delver.comhttp://www.musicglue.com=
http://www.castleproject.orghttp://www.idcc.co.il-=D7=94=D7=9B=D7=A0=D7=A1 =
=D7=94=D7=A7=D7=94=D7=99=D7=9C=D7=AA=D7=99 =D7=94=D7=A8=D7=90=D7=A9=D7=95=
=D7=9F =D7=9C=D7=9E=D7=A4=D7=AA=D7=97=D7=99 =D7=93=D7=95=D7=98=D7=A0=D7=98 =
- =D7=91=D7=95=D7=90=D7=95 =D7=91=D7=94=D7=9E=D7=95=D7=A0=D7=99=D7=9B=D7=9D
>
> --
> 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@goog=
legroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/norm-mongodb?hl=3Den.
>
>

--0016e64bde324488c904889d4468
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

I&#39;ve been working on a project which has the exact same requirements. H=
ere&#39;s how I solved it.<div><br></div><div>I have a proper internal ID u=
sing ObjectId, but for referencing from URLs and whatnot I created as secon=
d property called PublicId. Then I just generate a random 6 character alpha=
numeric string and try to get it from the database. If one exists, then I&#=
39;ll just regen that id,=C2=A0otherwise=C2=A0I insert the entry with that =
ID. Chances of a conflict / race condition are utterly small.</div>

<div><br></div><div>The actual code is below.</div><div><br clear=3D"all">B=
est regards,<br>Michael<br><br></div><div>Note:=C2=A0UniqunessMethod is a d=
elegate I pass in which basically does a select from mongodb where publicid=
 =3D=3D random key. But I use a delegate so I can run this for any entity I=
 want.</div>

<div><br></div><div><div><div>private string GetNewPublicId&lt;TEntity&gt;(=
Func&lt;string, TEntity&gt; UniqunessMethod)</div><div><span class=3D"Apple=
-tab-span" style=3D"white-space:pre">	</span>where TEntity : class</div><di=
v>

{</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</spa=
n>Random rand =3D new Random();</div><div><span class=3D"Apple-tab-span" st=
yle=3D"white-space:pre">	</span>bool unique =3D false;</div><div><span clas=
s=3D"Apple-tab-span" style=3D"white-space:pre">	</span>string id =3D null;<=
/div>

<div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">	</span>while (!unique)</div><div><span class=3D"Apple-tab-span" style=3D=
"white-space:pre">	</span>{</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">		</span>StringBuilder sb =3D new StringBuilder();</di=
v>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>for =
(int i =3D 0; i &lt; 6; i++)</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">		</span>{</div><div><span class=3D"Apple-tab-span" st=
yle=3D"white-space:pre">			</span>int next =3D rand.Next(0, 36);</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>if =
(next &lt; 10)</div><div><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre">			</span>{</div><div><span class=3D"Apple-tab-span" style=3D"white-=
space:pre">				</span>sb.Append(next);</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>}</=
div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span=
>else</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		=
	</span>{</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">				</span>ne=
xt -=3D 10;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">				</span>next =3D &#39;a&#39; + next;</div><div><span class=3D"Apple-=
tab-span" style=3D"white-space:pre">				</span>sb.Append((char) next);</div=
>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>}</=
div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>=
}</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">		</span>id =3D sb.ToString();</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>TEnt=
ity entity =3D UniqunessMethod(id);</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">		</span>unique =3D entity =3D=3D null;</div><d=
iv><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</div>

<div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">	</span>return id;</div><div>}</div></div></div><div><br>
<br><br><div class=3D"gmail_quote">On Tue, Jun 8, 2010 at 8:50 AM, SaraC <s=
pan dir=3D"ltr">&lt;<a href=3D"mailto:sarajchi...@gmail.com" target=3D"_bla=
nk">sarajchi...@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex">


The problem is this application is a url shortener and requires an<br>
identifier that can be hashed and<br>
used for those purposes. I don&#39;t know of another solution while<br>
ensuring uniqueness.<br>
<div><br>
On Jun 8, 2:49=C2=A0am, Ken Egozi &lt;<a href=3D"mailto:egoz...@gmail.com" =
target=3D"_blank">egoz...@gmail.com</a>&gt; wrote:<br>
&gt; select MAX is definitely wrong for the reasons mentioned.<br>
&gt; I would avoid Identity behaviour anyway, and not only because of the<b=
r>
&gt; sharding issue. Using Identity means that every Insert must immediatel=
y call<br>
&gt; the database. This is not always desirable, as you might want to do mo=
re<br>
&gt; stuff with the Id, but postpone the DB call for later for various reas=
ons.<br>
&gt;<br>
&gt; Now using the ObjectId approach has its limitations also, mainly the s=
ize<br>
&gt; and the readability, as some systems like human readable ids (order nu=
mber,<br>
&gt; user id, customer service call id, etc.)<br>
&gt; you can enjoy both worlds if you employ the hi-lo algorithm (or<br>
&gt; a variation thereof). You get an integer id, generally sequential, fas=
t to<br>
&gt; generate and simple to implement.<br>
&gt;<br>
&gt; The docs for Hibernate/NHibernate are all full with reasons to avoid<b=
r>
&gt; Identity and use GuidComb (their equivalence of ObjectId) or HiLo<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div><div>&gt; On Tue, Jun 8, 2010 at 9:35 AM, J=C3=B8rn Wildt &lt;<a href=
=3D"mailto:j...@fjeldgruppen.dk" target=3D"_blank">j...@fjeldgruppen.dk</a>=
&gt; wrote:<br>
&gt; &gt; Check this request for some thoughts:<br>
&gt; &gt;<a href=3D"http://jira.mongodb.org/browse/SERVER-195" target=3D"_b=
lank">http://jira.mongodb.org/browse/SERVER-195</a><br>
&gt;<br>
&gt; &gt; Beware of concurrency issues: two reades may read MAX() =3D 10 an=
d then<br>
&gt; &gt; both of them increment it to 11. You need to use MongoDB&#39;s va=
rious<br>
&gt; &gt; atomic operation to ensure this operation is safe.<br>
&gt;<br>
&gt; &gt; /J=C3=B8rn<br>
&gt;<br>
&gt; &gt; On Jun 8, 8:19 am, SaraC &lt;<a href=3D"mailto:sarajchi...@gmail.=
com" target=3D"_blank">sarajchi...@gmail.com</a>&gt; wrote:<br>
&gt; &gt; &gt; I&#39;m new to NoSQL, I&#39;m looking to either create an id=
entity field (that<br>
&gt; &gt; &gt; starts at 0) or build my own by selecting a max value.<br>
&gt;<br>
&gt; &gt; &gt; I understand that the Mongo Ids are timestamps and can be us=
ed as<br>
&gt; &gt; &gt; such. However, I need a smaller value for this column.<br>
&gt;<br>
&gt; &gt; &gt; Thanks a bunch!<br>
&gt;<br>
&gt; &gt; --<br>
&gt; &gt; You received this message because you are subscribed to the Googl=
e Groups<br>
&gt; &gt; &quot;NoRM mongodb&quot; group.<br>
&gt; &gt; To post to this group, send email to <a href=3D"mailto:norm-mongo=
db@googlegroups.com" target=3D"_blank">norm-mongodb@googlegroups.com</a>.<b=
r>
&gt; &gt; To unsubscribe from this group, send email to<br>
</div>&gt; &gt; <a href=3D"mailto:norm-mongodb%2Bunsubscr...@googlegroups.c=
om" target=3D"_blank">norm-mongodb+unsubscribe@googlegroups.com</a>&lt;norm=
-mongodb%2Bunsubscribe@google <a href=3D"http://groups.com" target=3D"_blan=
k">groups.com</a>&gt;<br>



<div>&gt; &gt; .<br>
&gt; &gt; For more options, visit this group at<br>
&gt; &gt;<a href=3D"http://groups.google.com/group/norm-mongodb?hl=3Den" ta=
rget=3D"_blank">http://groups.google.com/group/norm-mongodb?hl=3Den</a>.<br=
>
&gt;<br>
&gt; --<br>
</div>&gt; Ken Egozi.<a href=3D"http://www.kenegozi.com/bloghttp://www.delv=
er.comhttp://www.musicglue.comhttp://www.castleproject.orghttp://www.idcc.c=
o.il-" target=3D"_blank">http://www.kenegozi.com/bloghttp://www.delver.comh=
ttp://www.musicglue.comhttp://www.castleproject.orghttp://www.idcc.co.il-</=
a> =D7=94=D7=9B=D7=A0=D7=A1 =D7=94=D7=A7=D7=94=D7=99=D7=9C=D7=AA=D7=99 =D7=
=94=D7=A8=D7=90=D7=A9=D7=95=D7=9F =D7=9C=D7=9E=D7=A4=D7=AA=D7=97=D7=99 =D7=
=93=D7=95=D7=98=D7=A0=D7=98 - =D7=91=D7=95=D7=90=D7=95 =D7=91=D7=94=D7=9E=
=D7=95=D7=A0=D7=99=D7=9B=D7=9D<br>



<div><div></div><div><br>
--<br>
You received this message because you are subscribed to the Google Groups &=
quot;NoRM mongodb&quot; group.<br>
To post to this group, send email to <a href=3D"mailto:norm-mongodb@googleg=
roups.com" target=3D"_blank">norm-mongodb@googlegroups.com</a>.<br>
To unsubscribe from this group, send email to <a href=3D"mailto:norm-mongod=
b%2Bunsubscribe@googlegroups.com" target=3D"_blank">norm-mongodb+unsubscrib=
e@googlegroups.com</a>.<br>
For more options, visit this group at <a href=3D"http://groups.google.com/g=
roup/norm-mongodb?hl=3Den" target=3D"_blank">http://groups.google.com/group=
/norm-mongodb?hl=3Den</a>.<br>
<br>
</div></div></blockquote></div><br>
</div>

--0016e64bde324488c904889d4468--