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

find.sort() params no longer accept an array #12434

Closed
2 tasks done
penfold45 opened this issue Sep 15, 2022 · 4 comments · Fixed by #12483
Closed
2 tasks done

find.sort() params no longer accept an array #12434

penfold45 opened this issue Sep 15, 2022 · 4 comments · Fixed by #12483
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@penfold45
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.1

Node.js version

16

MongoDB server version

5.0.12

Description

Hi

I am just upgrading from 6.3.1 to 6.6.1 and I noticed that the sort() funciton params have changed
I can not really find any explenaiton why or even any mention of this change being expected i.e its not in the release notes

It appears to have been done in this commit 8d917a9, feels like a regression but maybe it was intentional?

I used to be able to do

        .sort([
            ['year', 1],
            ['quarter', 1],
            ['setNumber', 1],
        ])

But now it appears it has to be

        .sort({
            year: 1,
            quarter: 1,
            setNumber: 1,
        })

Steps to Reproduce

Attempt to pass an array of arrays to sort()

        .sort([
            ['year', 1],
            ['quarter', 1],
            ['setNumber', 1],
        ])

Expected Behavior

No response

@hasezoey
Copy link
Collaborator

hasezoey commented Sep 16, 2022

it seems like 8d917a9 changed the typing of sort from string | any to explicit string | { [key: string]: SortOrder }

but from what i can tell, arrays were never supported (at least not mentioned in the documentation), see API Docs: Query.prototype.sort()

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Sep 16, 2022
@penfold45
Copy link
Author

Hi I have noticed that the documentation was not entirly accurate in the past. However this is a particularly popular post of how to sort using mongoose https://stackoverflow.com/questions/4299991/how-to-sort-in-mongoose and as far as I am aware all of this worked until recently. It is also my underatanding that these are also all the ways you can sort directly in Monogodb itself.

@penfold45
Copy link
Author

Any thoughts on if this we be reverted back to allow what it used too or if you will keep the "new" format? Happy to update my code but dont really want to do it if there is no need as we rely on the previous array format a lot.

Thanks

@hasezoey
Copy link
Collaborator

hasezoey commented Sep 29, 2022

the behavior did not change in runtime, only the types got more strict / more defined, instead of a simple any it got stricter, and the array way was seemingly not considered

your options are currently:

  • ignore the errors with @ts-ingore or @ts-expect-error
  • downgrade to a version that still has the any typing

and then wait for a fix to be released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants