Skip to content

Commit

Permalink
feat(query): add options parameter to Query.prototype.sort()
Browse files Browse the repository at this point in the history
Fix #14365
  • Loading branch information
vkarpov15 committed Feb 24, 2024
1 parent 31e0a69 commit 4beb8bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/query.js
Expand Up @@ -2861,19 +2861,27 @@ Query.prototype.distinct = function(field, conditions) {
* Cannot be used with `distinct()`
*
* @param {Object|String|Array<Array<(string | number)>>} arg
* @param {Object} [options]
* @param {Boolean} [options.override=false] If true, replace existing sort options with `arg`
* @return {Query} this
* @see cursor.sort https://www.mongodb.com/docs/manual/reference/method/cursor.sort/
* @api public
*/

Query.prototype.sort = function(arg) {
if (arguments.length > 1) {
throw new Error('sort() only takes 1 Argument');
Query.prototype.sort = function(arg, options) {
if (arguments.length > 2) {
throw new Error('sort() takes at most 2 arguments');
}
if (options != null && typeof options !== 'object') {
throw new Error('sort() options argument must be an object or nullish');
}

if (this.options.sort == null) {
this.options.sort = {};
}
if (options && options.override) {
this.options.sort = {};
}
const sort = this.options.sort;
if (typeof arg === 'string') {
const properties = arg.indexOf(' ') === -1 ? [arg] : arg.split(' ');
Expand Down
25 changes: 24 additions & 1 deletion test/query.test.js
Expand Up @@ -795,7 +795,7 @@ describe('Query', function() {
e = err;
}
assert.ok(e, 'uh oh. no error was thrown');
assert.equal(e.message, 'sort() only takes 1 Argument');
assert.equal(e.message, 'sort() takes at most 2 arguments');

});
});
Expand Down Expand Up @@ -4191,4 +4191,27 @@ describe('Query', function() {
assert.strictEqual(doc.account.owner, undefined);
assert.strictEqual(doc.account.taxIds, undefined);
});

it('allows overriding sort (gh-14365)', function() {
const testSchema = new mongoose.Schema({
name: String
});

const Test = db.model('Test', testSchema);

const q = Test.find().select('name').sort({ name: -1, _id: -1 });
assert.deepStrictEqual(q.getOptions().sort, { name: -1, _id: -1 });

q.sort({ name: 1 }, { override: true });
assert.deepStrictEqual(q.getOptions().sort, { name: 1 });

q.sort(null, { override: true });
assert.deepStrictEqual(q.getOptions().sort, {});

q.sort({ _id: 1 }, { override: true });
assert.deepStrictEqual(q.getOptions().sort, { _id: 1 });

q.sort({}, { override: true });
assert.deepStrictEqual(q.getOptions().sort, {});
});
});
5 changes: 4 additions & 1 deletion types/query.d.ts
Expand Up @@ -715,7 +715,10 @@ declare module 'mongoose' {
slice(val: number | Array<number>): 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 | { $meta: any } } | [string, SortOrder][] | undefined | null): this;
sort(
arg?: string | { [key: string]: SortOrder | { $meta: any } } | [string, SortOrder][] | undefined | null,
options?: { override?: boolean }
): this;

/** Sets the tailable option (for use with capped collections). */
tailable(bool?: boolean, opts?: {
Expand Down

0 comments on commit 4beb8bc

Please sign in to comment.