Skip to main content

SORT

Short Description

MongoDB is sorting documents to satisfy your query’s order requirements

Detailed Description

In MongoDB explain plans, SORT is an execution stage that indicates MongoDB is sorting documents to satisfy your query’s order requirements.

In simple terms:

  • The SORT stage appears when you use the .sort() method in your query (for example, sorting movies by year or rating).
  • If MongoDB doesn’t have an index that supports the sort, it has to sort the documents in memory after retrieving them, which can be slower—especially with large datasets.

Why does it matter?

  • If you often see a standalone SORT stage in your explain plan, it’s a hint that you could create an index to support your common sorts. With a proper index, MongoDB can sort results while scanning the index, making the query more efficient and possibly eliminating the need for an in-memory sort.

Example:

db.movies.find().sort({ year: 1 })

If there’s no index on year, you’ll see a SORT stage in your explain plan.

Summary:

SORT means MongoDB is arranging query results according to your specified order, potentially in memory, if not supported by an index. Creating an index on the sorted fields can often speed up these queries.

Search online

If this article doesn't have the information you need you can try searching online. Remember, you can contribute suggestions to this page.