Get a Random Subset #3335
-
I am generating a REST API and I would like to have the ability to get a random subset. Similar functionality can be found at this free dog API. What would be your recommendation on how to implement this in Feathers? I thought about having something in the query string to trigger a before hook that would:
While implementing that solution I got stuck trying to add another query property in querySyntax(). I couldn't seem to figure out the exact formatting to allow my query property. Edit: To be clear this is the documentation example that I think is an analog for adding a query property. In the example case an '$ilike' property is added. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Which database adapter are you using? |
Beta Was this translation helpful? Give feedback.
-
The simplest algorithm I thought of is to get a limited number of data by $limit and sort the general collection by random field with $sort For example const field = random(['name', 'bio', 'age', 'created_at'])
const query {
$limit: 30,
$sort: {
[field]: 1
}
}
const result = client.service("dogs").find({query}) The |
Beta Was this translation helpful? Give feedback.
You can use a mongo aggregation pipeline to get your sample:
https://www.mongodb.com/docs/manual/reference/operator/aggregation/sample/