Details

    • Type: New Feature
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 3.1 Desired
    • Component/s: Indexing, Querying, Usability
    • Labels:
      None
    • Environment:
      Global, all environments

      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

        Activity

        Hide
        tomlor Thomas Lorimor added a comment -

        It seems like pulling random records could be accomplished easily by simply adding a new $random expression to the aggregation framework.

        Then you can, say, query for 5 random people of age 25:

        db.foo.aggregate(
        [{ $match :

        { age: 25 }

        },
        { $project : { rand : "$random" }},
        { $sort :

        { rand: 1 }

        },

        { $limit : 5 }

        ]
        );

        Show
        tomlor Thomas Lorimor added a comment - It seems like pulling random records could be accomplished easily by simply adding a new $random expression to the aggregation framework. Then you can, say, query for 5 random people of age 25: db.foo.aggregate( [{ $match : { age: 25 } }, { $project : { rand : "$random" }}, { $sort : { rand: 1 } }, { $limit : 5 } ] );
        Hide
        callumgare Callum Gare added a comment -

        If this does happen then a way to seed the random so a particular result can be reproduced if needed would be much appreciated. By me at least

        Show
        callumgare Callum Gare added a comment - If this does happen then a way to seed the random so a particular result can be reproduced if needed would be much appreciated. By me at least
        Hide
        amcgregor Alice Bevan-McGregor added a comment -

        +1 for a $random option to aggregation sort, but this would also be useful in normal queries. query.order_by('$rand') or some-such. Basically switch the sort mechanism from "quick sort" (or whatever is actually in use to a bogosort which randomizes the available choices.

        Show
        amcgregor Alice Bevan-McGregor added a comment - +1 for a $random option to aggregation sort, but this would also be useful in normal queries. query.order_by('$rand') or some-such. Basically switch the sort mechanism from "quick sort" (or whatever is actually in use to a bogosort which randomizes the available choices.
        Hide
        Jeroenooms Jeroen Ooms added a comment -

        +1 for built-in random sorting. Taking a random sample from a dataset is required by many applications in statistics and data-science in order to generalize properties of the sample to the larger dataset.

        Show
        Jeroenooms Jeroen Ooms added a comment - +1 for built-in random sorting. Taking a random sample from a dataset is required by many applications in statistics and data-science in order to generalize properties of the sample to the larger dataset.
        Hide
        codeninja Andrei Odeski added a comment -

        +1 For this feature we have been waiting for quite some time, lets push this for the next sub release like 3.1. Need a way to efficiently generate random records without using hacks like this db.yourCollection.find().limit(-1).skip(yourRandomNumber).next()

        Show
        codeninja Andrei Odeski added a comment - +1 For this feature we have been waiting for quite some time, lets push this for the next sub release like 3.1. Need a way to efficiently generate random records without using hacks like this db.yourCollection.find().limit(-1).skip(yourRandomNumber).next()

          Dates

          • Created:
            Updated:
            Days since reply:
            4 days ago
            Date of 1st Reply: