M
M
Mr_Sinister2016-06-29 00:16:31
Java
Mr_Sinister, 2016-06-29 00:16:31

How to select from a MongoDB collection of N random documents?

Faced the task of searching for N random documents in a MongoDB collection (with additional conditions on the fields).
1) Calling findOne N times on a pre-created random field is not a very optimal option
2) Using skip is not an option, since only the offset will be random here.
I found this option in the documentation
https://docs.mongodb.com/manual/reference/operator. ..
But the MongoDB driver for Java (spring-data-mongodb) specifically does not have this operator

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2016-06-29
@Mr_Sinister

stackoverflow.com/questions/35976725/random-docume...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question