Created
December 16, 2013 04:22
-
-
Save ctcarrier/7982265 to your computer and use it in GitHub Desktop.
Select a random document using
This file contains hidden or 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
import reactivemongo.bson.BSONDocument | |
import reactivemongo.api.collections.default.BSONCollection | |
import reactivemongo.api.{QueryOpts, DB} | |
import reactivemongo.core.commands.Count | |
import reactivemongo.api.collections.default._ | |
def getDocument(collection: BSONCollection): Future[Option[YourClass]] = { | |
val futureCount = db.command(Count(collection.name)) | |
futureCount.flatMap { count => | |
val skip = Random.nextInt(count) | |
collection.find(BSONDocument()).options(QueryOpts(skipN = skip)).one[YourClass] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment