How can i get random record from my single collection ?

631 views
Skip to first unread message

jignesh patel

unread,
Dec 23, 2009, 11:58:28 PM12/23/09
to mongodb-user
Hi,

I am doing testing on my large collection, there is a special need to
get single random record from my collection which is having a millions
of record in it ?

Thanks

Kristina Chodorow

unread,
Dec 24, 2009, 12:30:19 AM12/24/09
to mongod...@googlegroups.com
There's no built-in way.  You could do a count, generate a random number between 0 and the count, then do db.bigCollection.find().limit(-1).skip(randomNum).next().



--

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



jignesh patel

unread,
Dec 24, 2009, 1:16:30 AM12/24/09
to mongodb-user
Thank you Kristina......

On Dec 24, 10:30 am, Kristina Chodorow <krist...@10gen.com> wrote:
> There's no built-in way.  You could do a count, generate a random number
> between 0 and the count, then do
> db.bigCollection.find().limit(-1).skip(randomNum).next().
>

> On Wed, Dec 23, 2009 at 11:58 PM, jignesh patel <jignesh.dadhan...@gmail.com


>
> > wrote:
> > Hi,
>
> > I am doing testing on my large collection, there is a special need to
> > get single random record from my collection which is having a millions
> > of record in it ?
>
> > Thanks
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages