Selecting (max) OR identity field

48 views
Skip to first unread message

SaraC

unread,
Jun 8, 2010, 2:19:31 AM6/8/10
to NoRM mongodb


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!

Jørn Wildt

unread,
Jun 8, 2010, 2:35:23 AM6/8/10
to NoRM mongodb
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

Ken Egozi

unread,
Jun 8, 2010, 2:49:51 AM6/8/10
to Jørn Wildt, NoRM mongodb
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




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




--
Ken Egozi.
http://www.kenegozi.com/blog
http://www.delver.com
http://www.musicglue.com
http://www.castleproject.org
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Michael Hawksworth

unread,
Jun 8, 2010, 2:53:15 AM6/8/10
to Jørn Wildt, NoRM mongodb
As a newbie to MongoDB myself I struggled with this for a while. 

Eventually I realised the only reason for using this was my MSSQL fixation (Interestingly the is a nasty bug in MSSQL Identity anyway) so I just started using an ObjectID field and everything seems fine (so far).

OK uses somewhat more disk space but for my app that's not really an issue.

Regards
Michael Hawksworth


Henning

unread,
Jun 8, 2010, 4:19:05 AM6/8/10
to NoRM mongodb
Diskspace is usually a non-issue. Harddrives is the cheapest component
you can buy.

Henning
> > norm-mongodb...@googlegroups.com<norm-mongodb%2Bunsubscribe@google­groups.com>
> > .

SaraC

unread,
Jun 8, 2010, 11:50:00 AM6/8/10
to NoRM mongodb
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.
> > norm-mongodb...@googlegroups.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.comhttp://www.castleproject.orghttp://www.idcc.co.il- הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Michael Kennedy

unread,
Jun 9, 2010, 2:42:02 PM6/9/10
to SaraC, NoRM mongodb
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;
}



To unsubscribe from this group, send email to norm-mongodb...@googlegroups.com.

Jørn Wildt

unread,
Jun 9, 2010, 3:10:04 PM6/9/10
to Michael Kennedy, SaraC, NoRM mongodb
> Chances of a conflict / race condition are utterly small.

If you furthermore add a unique index on that property then you are
guaranteed that no duplicates exists.

/Jørn

Andrew Theken

unread,
Jun 9, 2010, 3:14:50 PM6/9/10
to Jørn Wildt, Michael Kennedy, SaraC, NoRM mongodb
Adam is working on implementing HiLo for each collection name, we'll have something to deal with this in the repo in the next few days.

//Andrew Theken


Sara Chipps

unread,
Jun 9, 2010, 3:19:40 PM6/9/10
to Andrew Theken, Jørn Wildt, Michael Kennedy, NoRM mongodb
wow, ok, keep me posted. thanks so much for the help, guys, I will let you know how it turns out. 

On Wed, Jun 9, 2010 at 3:14 PM, Andrew Theken <ath...@gmail.com> wrote:
Adam is working on implementing HiLo for each collection name, we'll have something to deal with this in the repo in the next few days.

//Andrew Theken


On Wed, Jun 9, 2010 at 3:10 PM, Jørn Wildt <j...@fjeldgruppen.dk> wrote:
Chances of a conflict / race condition are utterly small.

If you furthermore add a unique index on that property then you are guaranteed that no duplicates exists.

/Jørn

----- Original ----- From: "Michael Kennedy" <mkennedy...@gmail.com>




--
Sara J Chipps
Developer. http://Bundl.it
Blogger. http://GirlDeveloper.com
Emailer. saraj...@gmail.com
Problem Solver. 862.812.4490

Andrew Theken

unread,
Jun 10, 2010, 7:25:30 AM6/10/10
to NoRM mongodb
Please see this thread:
http://groups.google.com/group/norm-mongodb/browse_thread/thread/e237d3e3233c0127

I have merged the changes Adam has made into my master, so give it a
whirl and let the group know how it goes.

-att

On Jun 9, 3:19 pm, Sara Chipps <sarajchi...@gmail.com> wrote:
> wow, ok, keep me posted. thanks so much for the help, guys, I will let you
> know how it turns out.
>
>
>
>
>
> On Wed, Jun 9, 2010 at 3:14 PM, Andrew Theken <athe...@gmail.com> wrote:
> > Adam is working on implementing HiLo for each collection name, we'll have
> > something to deal with this in the repo in the next few days.
>
> > //Andrew Theken
>
> > On Wed, Jun 9, 2010 at 3:10 PM, Jørn Wildt <j...@fjeldgruppen.dk> wrote:
>
> >>  Chances of a conflict / race condition are utterly small.
>
> >> If you furthermore add a unique index on that property then you are
> >> guaranteed that no duplicates exists.
>
> >> /Jørn
>
> >> ----- Original ----- From: "Michael Kennedy" <mkennedy66996...@gmail.com>
> >> To: "SaraC" <sarajchi...@gmail.com>
> >> norm-mongodb...@googlegroups.com<norm-mongodb%2Bunsubscribe@google groups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/norm-mongodb?hl=en.
>
> --
> Sara J Chipps
> Developer.http://Bundl.it
> Blogger.http://GirlDeveloper.com
> Emailer. sarajchi...@gmail.com
> Problem Solver. 862.812.4490
Reply all
Reply to author
Forward
0 new messages