Random record from MongoDB

后端 未结 27 1914
栀梦
栀梦 2020-11-22 01:22

I am looking to get a random record from a huge (100 million record) mongodb.

What is the fastest and most efficient way to do so? The data is already t

27条回答
  •  无人共我
    2020-11-22 01:50

    Update for MongoDB 3.2

    3.2 introduced $sample to the aggregation pipeline.

    There's also a good blog post on putting it into practice.

    For older versions (previous answer)

    This was actually a feature request: http://jira.mongodb.org/browse/SERVER-533 but it was filed under "Won't fix."

    The cookbook has a very good recipe to select a random document out of a collection: http://cookbook.mongodb.org/patterns/random-attribute/

    To paraphrase the recipe, you assign random numbers to your documents:

    db.docs.save( { key : 1, ..., random : Math.random() } )
    

    Then select a random document:

    rand = Math.random()
    result = db.docs.findOne( { key : 2, random : { $gte : rand } } )
    if ( result == null ) {
      result = db.docs.findOne( { key : 2, random : { $lte : rand } } )
    }
    

    Querying with both $gte and $lte is necessary to find the document with a random number nearest rand.

    And of course you'll want to index on the random field:

    db.docs.ensureIndex( { key : 1, random :1 } )
    

    If you're already querying against an index, simply drop it, append random: 1 to it, and add it again.

提交回复
热议问题