Random record from MongoDB

后端 未结 27 1757
栀梦
栀梦 2020-11-22 01:22

I am looking to get a random record from a huge (100 million record) mongodb.

What is the fastest and most efficient way to do so? The data is already t

相关标签:
27条回答
  • 2020-11-22 01:54

    Do a count of all records, generate a random number between 0 and the count, and then do:

    db.yourCollection.find().limit(-1).skip(yourRandomNumber).next()
    
    0 讨论(0)
  • 2020-11-22 01:54

    When I was faced with a similar solution, I backtracked and found that the business request was actually for creating some form of rotation of the inventory being presented. In that case, there are much better options, which have answers from search engines like Solr, not data stores like MongoDB.

    In short, with the requirement to "intelligently rotate" content, what we should do instead of a random number across all of the documents is to include a personal q score modifier. To implement this yourself, assuming a small population of users, you can store a document per user that has the productId, impression count, click-through count, last seen date, and whatever other factors the business finds as being meaningful to compute a q score modifier. When retrieving the set to display, typically you request more documents from the data store than requested by the end user, then apply the q score modifier, take the number of records requested by the end user, then randomize the page of results, a tiny set, so simply sort the documents in the application layer (in memory).

    If the universe of users is too large, you can categorize users into behavior groups and index by behavior group rather than user.

    If the universe of products is small enough, you can create an index per user.

    I have found this technique to be much more efficient, but more importantly more effective in creating a relevant, worthwhile experience of using the software solution.

    0 讨论(0)
  • Now you can use the aggregate. Example:

    db.users.aggregate(
       [ { $sample: { size: 3 } } ]
    )
    

    See the doc.

    0 讨论(0)
提交回复
热议问题