Tevpro insights
How to sort zero or null values last in MongoDB
How do you sort documents by price, but ensure the ones with zero (0) or a NULL price values are always sorted LAST in the list?
Key takeaways
- A plain ascending price sort puts null before numeric values and zero before positive prices; it will not place both last.
- For an occasional query, compute a priceLast flag with $ifNull and $eq, then sort by the flag, price, and _id.
- For frequent or large sorts, maintain a stored sort flag and a matching compound index; a computed flag before $sort cannot use the index for that sort.
To sort prices from lowest to highest while placing zero, null, and missing prices last, sort by a priority field before sorting by price. Use a computed field for an occasional query, or store and index that priority field for a high-traffic query.
The Question
How do you sort documents by price, but ensure the ones with zero (0) or a NULL price values are always sorted LAST in the list?
A plain ascending price sort puts null before numeric prices and zero before positive prices. It cannot, by itself, put both zero and null last:
db.collection("kites")
.find()
.sort({ price: 1 })
.toArray();
The original approach stores a boolean hasPrice field and sorts true before false. It still works if hasPrice is false for zero, null, and missing prices, and stays in sync whenever price changes:
db.collection("kites")
.find()
.sort({ hasPrice: -1, price: 1, _id: 1 })
.toArray();
Compute the priority field when you do not need to store it
For an occasional sort, $set can calculate a temporary priceLast flag. $ifNull treats null and missing prices as zero; $eq then makes those values and a numeric zero true. Sort false first, then by price, and use _id to make ties consistent:
db.collection("kites")
.aggregate([
{
$set: {
priceLast: { $eq: [{ $ifNull: ["$price", 0] }, 0] }
}
},
{ $sort: { priceLast: 1, price: 1, _id: 1 } },
{ $unset: "priceLast" }
])
.toArray();
For numeric prices, this orders nonzero prices from lowest to highest, followed by null or missing prices and then zero. If negative prices are allowed, they remain among the nonzero prices. The final two groups are both last; their relative order follows the price sort. This assumes price is a number, null, or missing, not a mix of strings and numbers.
Keep the stored field for frequent or large sorts
The computed flag is convenient, but a $set stage before $sort means that sort cannot use a collection index. For a frequent query or large result set, keep the stored hasPrice approach and consider a compound index matching the sort order:
db.collection("kites").createIndex({
hasPrice: -1,
price: 1,
_id: 1
});
Update hasPrice whenever price changes, including transitions to or from zero and null. The name hasPrice is historical here: a zero price exists, but must be classified with the last group. For a new schema, a name such as priceLast would describe the sort rule more clearly. Check the actual query plan with explain() before treating an index as a performance win.
Sources
Why work with us
Why Tevpro?
Whether you’re a startup with a bold product idea or an established company seeking a stronger delivery partner, Tevpro delivers results. Our expert consultants specialize in building secure, scalable applications that simplify operations and drive real ROI.
FAQ
Frequently asked questions
MongoDB sorts null below numeric values, and zero sorts before positive numbers. An ascending price sort cannot put both last on its own; sort by a priority flag first, then by price.


