The $sortArray operator sorts elements inside an array field and returns a new sorted array. Use it for ordered tag lists, ranked scores, or product line items sorted by price — all within a single document.
01
Sort In-Place
Array field order.
02
input + sortBy
Two-part syntax.
03
1 / -1
Primitives sort.
04
Object arrays
Sort by field.
05
Use Cases
Tags, scores, items.
06
vs $sort
Array vs docs.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $sortArray operator takes an array and a sort specification, then returns a new sorted array. For example, sorting scores descending: { $sortArray: { input: "$scores", sortBy: -1 } } turns [40, 90, 70] into [90, 70, 40].
Unlike the pipeline $sort stage (which orders documents), $sortArray works on array elements within each document. It is available in MongoDB 5.2 and later.
💡
Beginner Tip
Chain with $slice after sorting to get the top N items: sort by score descending, then slice the first 3.
Foundation
📝 Syntax
The $sortArray operator uses an object with input and sortBy:
MongoDB applies grade first, then uses name to break ties — same rules as collection-level $sort.
Applications
🚀 Use Cases
Leaderboards — sort scores and slice the top N per player or team.
Catalog display — order line items or variants by price or priority inside an order document.
Tag and category lists — present alphabetized or ranked tags in the UI.
Data normalization — produce consistent array order before comparison with $setEquals.
🧠 How $sortArray Works
1
MongoDB evaluates the input array
The input expression resolves to an array field or computed result.
Input
2
sortBy rules are applied
Primitives use 1/-1. Object arrays use field paths with the same ordering semantics as $sort.
Sort
3
A new sorted array is returned
The original array field is not modified unless you assign the output to replace it.
Output
=
📊
Sorted array
Chain with $slice, $first, or store as a display field.
Wrap Up
Conclusion
The $sortArray operator sorts elements within an array field using input and sortBy. Use 1 or -1 for primitive arrays and field objects for arrays of documents.
Remember it sorts array contents, not whole documents — that is the job of the $sort stage. Next in the series: $split.
Use $sortArray when order matters inside one document’s array
Combine with $slice for top-N leaderboards
Use multi-field sortBy when objects share the same primary value
Default missing arrays with $ifNull before sorting
Verify MongoDB 5.2+ if $sortArray is unavailable
❌ Don’t
Confuse $sortArray with the pipeline $sort stage
Expect in-place mutation — assign the result to update the field
Use field-object sortBy on primitive arrays
Forget that null input returns null
Sort huge arrays on every request if a pre-sorted cached field works better
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $sortArray
Use these points when sorting arrays inside MongoDB documents.
5
Core concepts
📊01
Sort array
In-document order.
Purpose
📝02
input + sortBy
Two keys.
Syntax
🛠03
1 / -1
Primitives.
Rule
🗃04
{ field: 1 }
Object arrays.
Objects
⚠05
vs $sort
Array vs docs.
Important
❓ Frequently Asked Questions
$sortArray returns a new array with elements sorted according to a sort specification. It works on arrays of primitives (numbers, strings) or arrays of documents. The original array field is not modified unless you write the result back.
The syntax is { $sortArray: { input: <array expression>, sortBy: <sort spec> } }. For arrays of primitives, sortBy is 1 (ascending) or -1 (descending). For arrays of objects, sortBy is an object like { price: 1, name: -1 }.
The $sort stage reorders entire documents in the pipeline. $sortArray sorts elements within a single array field on each document. Use $sortArray when you need a sorted list inside one document.
Yes. When the input array contains objects, pass a sortBy object with multiple keys. MongoDB applies the first key, then breaks ties with the second, similar to multi-field $sort on collections.
If input is null or missing, $sortArray returns null. The input must resolve to an array. Use $ifNull to default to an empty array when needed.