Home:ALL Converter>Possible to retrieve multiple random, non-sequential documents from MongoDB?

Possible to retrieve multiple random, non-sequential documents from MongoDB?

Ask Time:2014-12-08T06:49:07         Author:Chad Johnson

Json Formatter

I'd like to retrieve a random set of documents from a MongoDB database. So far after lots of Googling, I've only seen ways to retrieve one random document OR a set of documents starting at a random skip position but where the documents are still sequential.

I've tried mongoose-simple-random, and unfortunately it doesn't retrieve a "true" random set. What it does is skip to a random position and then retrieve n documents from that position.

Instead, I'd like to retrieve a random set like MySQL does using one query (or a minimal amount of queries), and I need this list to be random every time. I need this to be efficient -- relatively on par with such a query with MySQL. I want to reproduce the following but in MongoDB:

SELECT * FROM products ORDER BY rand() LIMIT 50;

Is this possible? I'm using Mongoose, but an example with any adapter -- or even a straight MongoDB query -- is cool.

I've seen one method of adding a field to each document, generating a random value for each field, and using {rand: {$gte:rand()}} each query we want randomized. But, my concern is that two queries could theoretically return the same set.

Author:Chad Johnson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27348811/possible-to-retrieve-multiple-random-non-sequential-documents-from-mongodb
dotpush :

You may do two requests, but in an efficient way :\n\n\nYour first request just gets the list of all \"_id\" of document of your collections. Be sure to use a mongo projection db.products.find({}, { '_id' : 1 }).\nYou have a list of \"_id\", just pick N randomly from the list. \nDo a second query using the $in operator.\n\n\nWhat is especially important is that your first query is fully supported by an index (because it's \"_id\"). This index is likely fully in memory (else you'd probably have performance problems). So, only the index is read while running the first query, and it's incredibly fast.\n\nAlthough the second query means reading actual documents, the index will help a lot.\n\nIf you can do things this way, you should try.",
2014-12-08T02:03:42
yy