Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-533

Aggregation stage to randomly sample documents

    • Fully Compatible
    • Quint Iteration 6

      We've decided to go about this by adding a new aggregation stage: $sample. Given a positive integer, the stage will pseudo-randomly choose that number of documents from the incoming stream of documents, which is implicitly the entire collection when $sample is the first stage in the pipeline.

      Note that this ticket will only track the aggregation stage functionality, and this implementation will be very slow until SERVER-19183 and SERVER-19182 are resolved.

      Original description:
      Picking a random item from a collection is used in many cases. For example, you want a random item from the collection photos. Currently this can be accomplished by counting the resulting query, computing a random index within that count, and then getting that item with that random index.

      A easier approach would be requesting a random item directly from mongo given a query

      photos.find(

      {"author":"johndoe"}

      ).random()
      // this would act like .next() but instead would simply return a random item that matches the query

      photos.random_one(

      {"author":"johndoe"}

      )
      // this would act just like find_one, except it would return a random item that matches the query