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

$match description does not say anything about sorting #491

Closed
gregsdennis opened this issue Nov 14, 2023 · 2 comments · Fixed by #503
Closed

$match description does not say anything about sorting #491

gregsdennis opened this issue Nov 14, 2023 · 2 comments · Fixed by #503
Assignees

Comments

@gregsdennis
Copy link
Contributor

The docs for $match does say anything about sorting, and yet there is this test:

title:    $match, 2 matches, ordered by lexcial sorting of property names
context:  {cond: 3}
template: {$match: {'cond == 3': 2, 'cond == 5': 3, 'cond < 5 && cond > 0': 4, 'true': 17}}
result:   [4, 2, 17]

Also, what is the point of sorting in this case? If anything, shouldn't the results be sorted by the order they are in the template (which, depending on the language you're using, may be indeterministic).

@djmitche
Copy link
Contributor

As you mentioned, JSON data structures do not define an order on keys in an object, so it's impossible to order conditions as they were given in the template. But, we want the behavior to be deterministic. So the only alternative is to sort them.

This was kind of a flaw in $match, which IIRC is why $switch was introduced. $switch enforces that at most one arm matches, so the order in which they are checked does not matter.

@djmitche
Copy link
Contributor

(The $match docs should mention the sorting!)

@djmitche djmitche self-assigned this Nov 23, 2023
@djmitche djmitche changed the title $match description does say anything about sorting $match description does not say anything about sorting Nov 23, 2023
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 a pull request may close this issue.

2 participants