Details

    • Backport:
      No
    • # Replies:
      49
    • Last comment by Customer:
      true

      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
        Ryan Hamilton-Schumacher
        added a comment -

        @Antonio there is nothing to merge. See the Fix Version === "planned but not scheduled"

        Show
        Ryan Hamilton-Schumacher
        added a comment - @Antonio there is nothing to merge. See the Fix Version === "planned but not scheduled"
        Hide
        Antonio C Nalesso
        added a comment -

        Dear Ryan,
        Does it mean that somebody would have to implement this feature, or it is already implemented?
        I am sorry for my lack of knowledge and thanks anyway for your reply.

         

        On Tuesday, 14 January 2014, 2:23, 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=481474#comment-481474 ]

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

        @Antonio there is nothing to merge. See the Fix Version === "planned but not scheduled"
                       


        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
        Antonio C Nalesso
        added a comment - Dear Ryan, Does it mean that somebody would have to implement this feature, or it is already implemented? I am sorry for my lack of knowledge and thanks anyway for your reply.   On Tuesday, 14 January 2014, 2:23, 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=481474#comment-481474 ] Ryan Hamilton-Schumacher commented on SERVER-533 : ------------------------------------------------- @Antonio there is nothing to merge. See the Fix Version === "planned but not scheduled"                 – 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
        Ryan Hamilton-Schumacher
        added a comment -

        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

        Show
        Ryan Hamilton-Schumacher
        added a comment - 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
        Hide
        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
        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
        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
        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.

          Dates

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