Details

      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
        acnalesso Antonio C Nalesso added a comment -

        Hi there,
        Thanks for your reply.

        I have tried some of the solution from that stackoverflow link, but they seem not to work properly.
        On e solution I came up with was
        Model.offset(Model.count).limit(1).first

        This seems to be working fine, however, I am aware that it may not scale right?
        I am willing to design/write this feature for mongodb, could you give me some insight?
        I've started reading mongodb source code but I'm a bit lost. Any ideas would be very helpful.

        Thanks you

        with regards,
        Nalesso Antonio

        On Tuesday, 14 January 2014, 7:19, Ryan Hamilton-Schumacher (JIRA) <jira@mongodb.org> wrote:

            [ https://jira.mongodb.org/browse/SERVER-533?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=481598#comment-481598 ]

        Ryan Hamilton-Schumacher commented on SERVER-533:
        -------------------------------------------------

        Someone has to implement it, but I would guess it will take some time in both design and execution to do it right. I would not hold your breath but use one of the various alternatives http://stackoverflow.com/questions/2824157/random-record-from-mongodb/5517206
                       


        This message is automatically generated by JIRA.
        If you think it was sent incorrectly, please contact your JIRA administrators
        For more information on JIRA, see: http://www.atlassian.com/software/jira

        Show
        acnalesso Antonio C Nalesso added a comment - Hi there, Thanks for your reply. I have tried some of the solution from that stackoverflow link, but they seem not to work properly. On e solution I came up with was Model.offset(Model.count).limit(1).first This seems to be working fine, however, I am aware that it may not scale right? I am willing to design/write this feature for mongodb, could you give me some insight? I've started reading mongodb source code but I'm a bit lost. Any ideas would be very helpful. Thanks you with regards, Nalesso Antonio On Tuesday, 14 January 2014, 7:19, Ryan Hamilton-Schumacher (JIRA) <jira@mongodb.org> wrote:     [ https://jira.mongodb.org/browse/SERVER-533?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=481598#comment-481598 ] Ryan Hamilton-Schumacher commented on SERVER-533 : ------------------------------------------------- Someone has to implement it, but I would guess it will take some time in both design and execution to do it right. I would not hold your breath but use one of the various alternatives http://stackoverflow.com/questions/2824157/random-record-from-mongodb/5517206                 – This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira
        Hide
        jrschumacher Ryan Hamilton-Schumacher added a comment -

        Post a new question on Stackoverflow or use the Google+ community page, this is not the place for these questions.

        Show
        jrschumacher Ryan Hamilton-Schumacher added a comment - Post a new question on Stackoverflow or use the Google+ community page, this is not the place for these questions.
        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.

          Dates

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