Get random items

6 views
Skip to first unread message

fehaar

unread,
Sep 28, 2010, 4:58:20 AM9/28/10
to NoRM mongodb
Is it possible with NoRM to get a random set of results back from a
query? Say I want to display three random news items on my front page.

Thomas Gravgaard

Dmitry Aleksandrov

unread,
Sep 28, 2010, 9:46:38 AM9/28/10
to NoRM mongodb
Hi,

You can use .Skip(<<randrom_int_here>>).Take(3) calls

Regards,
Dmitry

fehaar

unread,
Sep 28, 2010, 10:42:48 AM9/28/10
to NoRM mongodb
Yes... That could do it in some cases. But it would always preserve
ordering.

When I used MSSQL i could use "ORDER BY NEWID()" and get a random
sequence out every time. A similar functionality could be useful.
Could it be done with some neat map/reduce function?

/ Thomas

On 28 Sep., 15:46, Dmitry Aleksandrov <alexandrov.dmi...@gmail.com>
wrote:

Ken Egozi

unread,
Sep 28, 2010, 11:04:30 AM9/28/10
to fehaar, NoRM mongodb
order by NewID can get extremly slow with large data sets

if you have sequential ids you can randomize three values, then ask them with an $in query

if there are a lot of gaps, you can issue three queries, each will ask for the first item with key >= random(minvalue, maxvalue). 
three get-by-id queries from Mongo should be at least as fast as using ORDER BY NEWID (on a large enough dataset)





--
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 - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

fehaar

unread,
Sep 28, 2010, 4:34:24 PM9/28/10
to NoRM mongodb
It is not because I am worried about speed or comparing with how
things are in SQL. I am using Mongo because it rocks - especially when
using NoRM. ;-)

So I am just trying to trying to solve this particular issue.

Right now I am not using sequential ID's. But that might be the way to
go if there are no other solutions to the problem.
> > 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- הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם
Reply all
Reply to author
Forward
0 new messages