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

Aggregation stage to randomly sample documents

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.1.6
    • Labels:
      None
    • Environment:
      Global, all environments
    • Backwards Compatibility:
      Fully Compatible
    • Epic Link:
    • Sprint:
      Quint Iteration 6

      Description

      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

        Attachments

          Issue Links

            Activity

              Dates

              • Created:
                Updated:
                Resolved: