MongoDB: what is the most efficient way to query a single random document? MongoDB: what is the most efficient way to query a single random document? mongodb mongodb

MongoDB: what is the most efficient way to query a single random document?


I had a similar issue once. In my case, I had a date property on my documents. I knew the earliest date possible in the dataset so in my application code, I would generate a random date within the range of EARLIEST_DATE_IN_SET and NOW and then query mongodb using a GTE query on the date property and simply limit it to 1 result.

There was a small chance that the random date would be greater than the highest date in the data set, so i accounted for that in the application code.

With an index on the date property, this was a super fast query.


It seems like you could mold solution 1 there, (assuming your _id key was an auto-inc value), then just do a count on your records, and use that as the upper limit for a random int in c++, then grab that row.

Likewise, if you don't have an autoinc _id key, just create one with your results.. having an additional field with an INT shouldn't add that much to your document size.

If you don't have an auto-inc field Mongo talks about how to quickly add one here:

Auto Inc Field.