Skip to content

Conversation

@singhankit001
Copy link

@singhankit001 singhankit001 commented Nov 29, 2025

This PR refactors Aggregate.prototype.sort to reuse the sort argument parsing logic from Query.prototype.sort, addressing an existing TODO in lib/aggregate.js
I extracted the sort parsing logic into a new shared helper function castSort (located in lib/helpers/query/castSort.js) and updated both Query and Aggregate to use it.
This reduces code duplication and ensures consistent behavior between the two classes. As a side effect, Aggregate.prototype.sort now supports Map and Array arguments, matching the capabilities of Query.prototype.sort.

Examples

Aggregate.prototype.sort now supports Maps and Arrays of Arrays, which were previously only supported by Query.prototype.sort:

// Using a Map
const map = new Map([['field', 'asc'], ['test', -1]]);
await Model.aggregate().sort(map); // Now works!

// Using an Array of Arrays
await Model.aggregate().sort([['field', 'asc'], ['test', -1]]); // Now works

@singhankit001
Copy link
Author

Hey @vkarpov15,
Please have a look at the PR and let me know if something needs to be changed.
Thank you

@singhankit001
Copy link
Author

@AbdelrahmanHafez @hasezoey Can you please have a look at the PR and let me know if anything needs to be changed.
Thank you

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

Looks mostly good to me, though this includes a bunch of style changes which should not be part of this PR; please fix that.

@singhankit001
Copy link
Author

singhankit001 commented Nov 30, 2025

@AbdelrahmanHafez @hasezoey Can you again run tests this time it would pass.

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

Looks good to me, though still contains some unrelated style changes (which arguably should be added to the lint, if possible).

Comment on lines +1353 to +1359
this.op === 'countDocuments' ||
this.op === 'find' ||
this.op === 'findOne' ||
this.op === 'deleteMany' ||
this.op === 'deleteOne' ||
this.op === 'findOneAndDelete' ||
this.op === 'remove') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Those are also unrelated style changes, though from what i can tell, it is not caught by the lint currently.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Overall this looks reasonable, but please undo the unrelated style changes

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Please undo all the unrelated style changes

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.

3 participants