Skip to content
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

feat: node list ordering[random, natural] added #111

Merged
merged 1 commit into from
Nov 18, 2021

Conversation

yilmazbahadir
Copy link
Contributor

@yilmazbahadir yilmazbahadir commented Nov 18, 2021

default order is random when the limit parameter is specified otherwise falls back to natural.

partly fixes #103

default order is random when the limit parameter is specified otherwise falls back to natural.
{ filter, limit, order }: NodeSearchCriteria = { filter: {}, limit: 0, order: NodeListOrder.Random },
): Promise<NodeDocument[]> => {
if (limit > 0 && order === NodeListOrder.Random) {
return Node.aggregate([{ $match: { ...filter } }, { $sample: { size: limit } }]).exec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that the $sample will not return the same document more than once in the result set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes =>
https://docs.mongodb.com/manual/reference/operator/aggregation/sample/

WiredTiger or in-memory storage engine, $sample does not return duplicate documents. WiredTiger is the default storage engine as of MongoDB 3.2.

@AnthonyLaw AnthonyLaw merged commit 9f549e5 into dev Nov 18, 2021
@AnthonyLaw AnthonyLaw deleted the feat/node-list-order branch November 18, 2021 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add the node list sorting
4 participants