Created
March 11, 2012 08:28
-
-
Save zakuni/2015601 to your computer and use it in GitHub Desktop.
Getting a random document from MongoDB collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = db.collections.count() | |
var rand = function(){return Math.floor( Math.random() * count )} | |
db.collection.find().limit(-1).skip(rand()).next(); |
agreed; we have something like ~8 million records in one database, and skipping deep in to the collection takes almost 2 seconds (depending on the skip value). not ideal in most production environments.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Skip is a very inefficient function in Mongodb.