-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
query.sort(null) should reset the sort options #14365
Comments
@vkarpov15 sorry for pinging, but maybe I'm missing something here? I'd like to be able to reset a query's sort so I can set something else on the same query instance |
To change sort options, you can try For example:
Just FYI, this is for mongoose@8.1.3 |
@FaizBShah Doesn't work, for example with a sort with 2 fields, and a reset, the previous fields can't be unset https://replit.com/@caub/mongoose-sort-up#index.js const mongoose = require('mongoose'); // 8.2.0
const testSchema = new mongoose.Schema({
name: String,
});
const Test = mongoose.model('Test', testSchema);
const q = Test.find().select('name').sort({ name: -1, _id: -1 });
console.log(q.getOptions());
// q.setOptions({});
q.setOptions({sort: {_id: 1}});
console.log(q.getOptions());
|
@caub @FaizBShah Hey guys, I am trying to work on some open source issues and I came across this one. I understand the use case you guys want to implement, and I think it can be supported by modifying this function here: In case you agree on how and if you are going to implement it, please reach out to me and I would love to take this up. Thanks :) |
@caub I think I found the solution. To override the current options object, you just need to pass the 2nd parameter (which stands for override) in
I hope this helps you!! |
@vkarpov15 Currently there is no mention of the Can I create a PR to add this? |
@FaizBShah Amazing find, thanks very much We should maybe do Maybe we could add a |
@caub @FaizBShah I put in PR #14375 with a potential fix, can you please let me know what you think? |
feat(query): add `options` parameter to `Query.prototype.sort()`
🚀 Feature Proposal
Calling
.sort(null)
should allow to unset whatever was set as a sort previously (inquery.options.sort
)Motivation
Sometimes it's useful to override default behaviors from plugins, later in the query chain
Similar to #14180
Example
I'm proposing
.sort(null)
, but I've tried using.setOptions(null)
or.setOptions({})
or.setOptions({sort: {_id: -1}})
without success. Any way would be fineThe text was updated successfully, but these errors were encountered: