S
S
Sergey Toy2011-07-22 21:33:59
NoSQL
Sergey Toy, 2011-07-22 21:33:59

MongoDB random 2 entries?

Good evening!
The question arose, how to randomly select 2 records from many in MongoDB?
In SQL I would do this:
SELECT * FROM sometable ORDER BY RAND() LIMIT 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sajgak, 2011-07-22
@Toy

collection.find().limit(1).skip(yourRandomNumber).next()
yourRandomNumber <= size_of(collection)

K
kastigar, 2011-07-23
@kastigar

Read this , namely the comments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question