I never worked with MongoDB from Python, but there is a general solution for your problem. Here is a MongoDB shell script for obtaining single random document:
N = db.collection.count(condition) db.collection.find(condition).limit(1).skip(Math.floor(Math.random()*N))
condition here is a MongoDB query. If you want to query an entire collection, use
query = null.
It’s a general solution, so it works with any MongoDB driver.
I ran a benchmark to test several implementations. First, I created test collection with 5567249 documents with indexed random field
I chose three methods to compare with each other:
I ran each method 10 times and got its average computing time:
method 1:882.1 msec method 2:1.2 msec method 3:0.6 msec
This benchmark shows that my solution not the fastest one.
But the third solution is not a good one either, because it finds the first element in database (sorted innatural order) with
rnd > random(). So, its output not truly random.
I think that second method is the best one for frequent usage. But it has one defect: it requires altering the whole database and ensuring additional index.