Automattic/mongoose · error · MongooseError
sort() takes at most 2 arguments
Error message
sort() takes at most 2 arguments
What it means
Query.prototype.sort() accepts exactly one sort specification plus one optional options object ({ override: true }); more than two arguments throws immediately. The guard primarily catches SQL-ORM-style (field, direction) calls such as .sort('createdAt', -1), which Mongoose does not support — direction must be encoded inside the spec, not passed as a second argument.
Source
Thrown at lib/query.js:3137
*
* // also possible is to use a array with array key-value pairs
* query.sort([['field', 'asc']]);
*
* #### Note:
*
* 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, options) {
if (arguments.length > 2) {
throw new MongooseError('sort() takes at most 2 arguments');
}
if (options != null && typeof options !== 'object') {
throw new MongooseError('sort() options argument must be an object or nullish');
}
if (this.options.sort == null) {
this.options.sort = {};
}
if (options?.override) {
this.options.sort = {};
}
const sort = this.options.sort;
if (typeof arg === 'string') {
const properties = arg.indexOf(' ') === -1 ? [arg] : arg.split(' ');
for (let property of properties) {
const ascend = '-' == property[0] ? -1 : 1;
if (ascend === -1) {
property = property.slice(1);View on GitHub (pinned to 49cdab0136)
Solutions
- Combine into one spec: .sort({ createdAt: -1 }) or the string form .sort('-createdAt')
- For multiple fields: .sort({ a: 1, b: -1 }) or .sort('a -b')
- The only valid second argument is an options object, e.g. .sort(spec, { override: true }) to replace an existing sort
Example fix
// before
const q = Model.find().sort('createdAt', -1);
// after
const q = Model.find().sort({ createdAt: -1 });
// or
const q = Model.find().sort('-createdAt'); Defensive patterns
Strategy: validation
Validate before calling
// Normalize (field, direction) input into a valid Mongoose sort spec
function toSort(field, dir) {
if (dir == null) return { [field]: 1 };
const d = String(dir).trim().toLowerCase();
return { [field]: d.startsWith('d') || d === '-1' ? -1 : 1 };
}
const q = Model.find().sort(toSort('createdAt', req.query.order)); Prevention
- Remember sort() takes one spec argument plus at most an options object
- Prefer object specs ({ field: -1 }) for dynamic sorting
- Add unit tests around any dynamic sort builder
When it happens
Trigger: .sort('createdAt', -1); .sort('name', 'desc'); passing a direction string or number as a second argument; three-argument calls like .sort(spec, {}, true).
Common situations: Developers coming from TypeORM/Sequelize/Knex where orderBy takes (field, direction); adapting MongoDB shell examples incorrectly; adding a second 'descending' argument by intuition.
Related errors
- sort() options argument must be an object or nullish
- Invalid sort() argument, must be array of arrays
- Invalid sort() argument. Must be a string, object, array, or
- Invalid addFields() argument. Must be an object
- Invalid select() argument. Must be string or object.
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/520be7ffb5d8f9f4.
Report an issue: GitHub.