Mongoose 9.13.0 was released on 23 November 2026, quickly followed by 9.13.1 on 24 November. The headline feature is a new schema option, defaultSort, that sets a model’s usual query order once instead of repeating .sort() in every query. The release also upgrades the MongoDB Node.js driver to 7.9 and adds a few performance fixes. Here’s how defaultSort works, where it doesn’t apply, and how to roll it out without slowing down your database.

What’s new in 9.13

From the 9.13.0 release notes:

  • MongoDB driver upgraded to 7.9
  • New defaultSort schema option that applies a default sort to queries unless you set one explicitly (#16676)
  • Faster id() lookups when setting a document array to a plain array, and a cheaper markModified() check on subdocuments
  • Stricter duck-typing checks for inherited and own properties
  • TypeScript support for nested projections

Upgrade:

npm install mongoose@^9.13

The problem defaultSort solves

Without an explicit sort, MongoDB doesn’t guarantee result order. Most apps work around this by remembering to add .sort({ createdAt: -1 }) everywhere, and pagination breaks quietly when someone forgets. defaultSort moves that rule into the schema.

Basic usage

The example below follows the Mongoose schema guide:

import mongoose, { Schema } from 'mongoose';

const itemSchema = new Schema(
  { name: String, price: Number },
  {
    timestamps: true,
    defaultSort: { createdAt: -1, _id: -1 },
  }
);

const Item = mongoose.model('Item', itemSchema);

// Sorted by { createdAt: -1, _id: -1 }
const latest = await Item.find({ price: { $gt: 0 } }).limit(10);

Adding _id as a second key is a good habit. createdAt values can tie, and _id breaks the tie so pages stay stable.

defaultSort accepts anything Query.prototype.sort() accepts, with two exceptions:

  1. $meta sorts such as { score: { $meta: 'textScore' } }, because MongoDB throws on $meta sorts for queries that don’t use $text.
  2. Empty sorts ({}, [], new Map() or a whitespace-only string). Setting defaultSort: null or false is allowed, to turn off a default on a discriminator.

Overriding and opting out

An explicit sort replaces the default completely. Mongoose doesn’t append the default as a tie-breaker:

// Sorted by { name: 1 } only
await Item.find().sort({ name: 1 });

// Run a single query with no sort at all
await Item.find().sort(null);
await Item.find({}, null, { sort: null });

The docs note that sort(undefined) and { sort: undefined } also run the query with no sort. That matters for API handlers that pass through an optional sort parameter:

// Careful: if req.query.sort is undefined, this disables defaultSort
await Item.find({}, null, { sort: req.query.sort });

// Better: only add the key when a sort was requested
const options = req.query.sort ? { sort: req.query.sort } : {};
await Item.find({}, null, options);

Which operations use it

According to the guide, defaultSort applies to:

  • find(), findOne(), findOneAndUpdate(), findOneAndReplace() and findOneAndDelete()
  • the queries that populate() runs

It does not apply to countDocuments(), estimatedDocumentCount(), distinct(), deleteOne(), deleteMany(), updateOne(), replaceOne(), updateMany(), exists(), bulkWrite() or aggregations.

updateOne() and replaceOne() are excluded on purpose. MongoDB only supports sort on those from MongoDB 8.0, and older servers throw if it’s set. On 8.0 or later, call .sort() yourself when it matters.

Some other rules worth knowing:

  • Mongoose applies defaultSort after pre middleware, so a middleware that calls sort() overrides it.
  • A discriminator schema can set its own defaultSort, or turn off the base one with false or null.
  • defaultSort is skipped for tailable cursors, and for queries with $near or $nearSphere, so geo queries keep their distance order.

Cleaner pagination with findAndCount()

Mongoose already had findAndCount() for limit and skip pagination. It returns the page and the total, and requires a sort so pages are consistent. With defaultSort, you no longer need to pass one:

async function listItems(page = 1, perPage = 20) {
  const [items, total] = await Item.findAndCount(
    {},
    null,
    { limit: perPage, skip: (page - 1) * perPage }
  );
  return { items, total, pages: Math.ceil(total / perPage) };
}

limit is still required. And findAndCount() throws if sort is explicitly empty, such as { sort: undefined }, because pagination without a sort can return inconsistent results.

How it affects populate()

The referenced model’s defaultSort affects populate() in three ways:

  • Arrays of refs like children: [{ type: ObjectId, ref: 'Child' }] keep the order of the IDs stored in the array. Pass options: { sort } to populate() to reorder them.
  • Populated virtuals are sorted by defaultSort.
  • With populate’s limit or perDocumentLimit, defaultSort decides which documents come back.
const authorSchema = new Schema({ name: String });
authorSchema.virtual('posts', {
  ref: 'Post',
  localField: '_id',
  foreignField: 'author',
});

// Post has defaultSort: { publishedAt: -1 }
const author = await Author.findOne({ name: 'Satish' })
  .populate({ path: 'posts', perDocumentLimit: 5 }); // the 5 newest posts

Don’t forget the index

The guide includes an important performance note. Without defaultSort, Model.findOne() with no filter just returns the first document it finds. With defaultSort, the same call has to sort the whole collection, which is slow on large collections unless an index covers the sort. Add one that matches:

itemSchema.index({ createdAt: -1, _id: -1 });

For filtered queries, put the equality fields first and the sort fields after:

itemSchema.index({ category: 1, createdAt: -1, _id: -1 });

Check with .explain() that hot queries use an index scan (IXSCAN) rather than an in-memory SORT stage:

const plan = await Item.find({ category: 'books' }).explain('executionStats');
console.dir(plan, { depth: null }); // look for IXSCAN, not a blocking SORT

Rollout checklist

  1. Upgrade to mongoose@^9.13 and run your tests.
  2. Add defaultSort to one model where order clearly matters, such as orders, posts or audit logs.
  3. Add a matching index and check it with explain().
  4. Search for handlers that pass { sort: something } where something might be undefined.
  5. Remove the now-redundant .sort() calls gradually, and keep explicit sorts where a query really needs a different order.

Takeaway

defaultSort in Mongoose 9.13 makes “newest first” (or any order) a property of the model instead of something every query has to remember. Use it with a tie-breaking _id, back it with an index, and remember that explicit sorts, including an accidental undefined, replace it entirely.