{"node_id": "f3466a67-2f95-11f1-8e6a-e86a64d24d78", "revisions": [{"id": "f3475882-2f95-11f1-9aad-e86a64d24d78", "node_id": "f3466a67-2f95-11f1-8e6a-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "mongodb: How to query a random row using pymongo\r\n=======================================================\r\n\r\n#. Get a count of every row.\r\n#. Create an offset in the range of 1 to count.\r\n#. Query for the item, skipping to the offset, limiting one result, and taking the [0]th index.\r\n \r\n.. code-block:: python\r\n\r\n    def getone( request ):\r\n    id = request.matchdict[ 'id' ]\r\n    if id == 'random':\r\n        from random import randrange\r\n        count  = request.db['people'].count()\r\n        offset = randrange( 1, count )\r\n        person = request.db['people'].find().skip( offset ).limit(1)[0]\r\n    else:\r\n        person = request.db['people'].find_one( { 'id': int(id) } )\r\n\r\n    return { 'person': person }", "source_format": "rst", "revision_number": 1, "created": 1307197679000}], "count": 1}