Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/query.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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