-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
fix(types): Allow sorting by text score #11893
Conversation
Sorting by text score is a special case. See https://www.mongodb.com/docs/manual/reference/method/cursor.sort/#text-score-metadata-sort This fixes a type error caused by 6.3.5
types/query.d.ts
Outdated
@@ -579,7 +579,7 @@ declare module 'mongoose' { | |||
snapshot(val?: boolean): this; | |||
|
|||
/** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */ | |||
sort(arg?: string | { [key: string]: SortOrder } | undefined | null): this; | |||
sort(arg?: string | { [key: string]: SortOrder } | { score: { $meta: 'textScore' } } | undefined | null): this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sort key doesn't necessarily have to be called score
. You can also do { textScore: { $meta: 'textScore' } }
, see https://thecodebarbarian.com/2014/04/10/a-nodejs-perspective-on-whats-new-in-mongodb-2-6-part-i-text-search.html .
According to https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/, { $meta: 'indexKey' }
is also valid.
Maybe something like { [key: string]: { $meta: 'textScore' | 'indexKey' } }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about the score
key. { $meta: 'indexKey' }
should be valid but not for sorting, it's just for the projection I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks 👍
Summary
Sorting by text score is a special case. See https://www.mongodb.com/docs/manual/reference/method/cursor.sort/#text-score-metadata-sort
This fixes a type error caused by 6.3.5
Examples
This code fails with 6.3.5:
It used to work