-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get 5 random rows from a model #431
Comments
There was some discussion here and here. There is a chainable |
after doing some research I was able to use this
|
im closing this issue, thanks again for your help. |
As dxg said, your solution works but is horrible on performance as it first sorts all rows in the db using random nunbers and only then selects elements. If your database server is close then you are probably better off using skip+limit and making multiple queries - will vastly reduce your database server load |
@spartan563 could you give me an example of how to do the |
I believe the idea is to do a Some pseudocode (sync design, would need to make it async for Node) var count = db.model.count(...);
var models = [];
while(models.length < n)
models.push(db.model.find().skip(Math.random() * count).limit(1)); |
orm-random - adds |
thank you @dresende |
I have a users model and I want to be able to get 5 random rows. @spartan563 do you have any suggestions on how to do this?
The text was updated successfully, but these errors were encountered: