quarkusio/quarkus · error · UnsupportedOperationException
Cannot sort by nulls first or nulls last
Error message
Cannot sort by nulls first or nulls last
What it means
When converting a Panache Sort to a MongoDB sort Document, any column with a non-null nullPrecedence (nulls first/last, from Sort.nullsFirst()/nullsLast()) throws UnsupportedOperationException because MongoDB's sort documents cannot express null-ordering semantics the way SQL does.
Source
Thrown at extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/ReactiveMongoOperations.java:609
return createQuery(collection, null, null);
}
public QueryType findAll(Class<?> entityClass, Sort sort) {
ReactiveMongoCollection collection = mongoCollection(entityClass);
Bson sortDoc = sortToDocument(sort);
return createQuery(collection, null, sortDoc);
}
private Bson sortToDocument(Sort sort) {
if (sort == null) {
return null;
}
Document sortDoc = new Document();
for (Sort.Column col : sort.getColumns()) {
sortDoc.append(col.getName(), col.getDirection() == Sort.Direction.Ascending ? 1 : -1);
if (col.getNullPrecedence() != null) {
throw new UnsupportedOperationException("Cannot sort by nulls first or nulls last");
}
}
return sortDoc;
}
public Uni<List<?>> listAll(Class<?> entityClass) {
return (Uni) list(findAll(entityClass));
}
public Uni<List<?>> listAll(Class<?> entityClass, Sort sort) {
return (Uni) list(findAll(entityClass, sort));
}
public Multi<?> streamAll(Class<?> entityClass) {
return stream(findAll(entityClass));
}
public Multi<?> streamAll(Class<?> entityClass, Sort sort) {View on GitHub (pinned to e1c734241f)
Solutions
- Remove nullsFirst()/nullsLast() from the Sort columns when targeting MongoDB
- If null ordering matters, store an explicit sortable sentinel field or use an aggregation pipeline with $ifNull/$sort instead of a simple sort
- Filter out null-valued fields in the query so ordering of nulls becomes irrelevant
Example fix
// before
Sort sort = Sort.descending("deletedAt").nullsLast("deletedAt"); // throws
// after
Sort sort = Sort.descending("deletedAt"); // MongoDB default null ordering Defensive patterns
Strategy: validation
Validate before calling
boolean hasNullPrecedence = sort.getColumns().stream().anyMatch(c -> c.getNullPrecedence() != null);
if (hasNullPrecedence) throw new IllegalArgumentException("nullsFirst/nullsLast not supported by MongoDB Panache"); Try / catch
try {
return repository.list(sort);
} catch (UnsupportedOperationException e) {
return repository.list(sortIgnoringNullPrecedence(sort));
} Prevention
- Never use Sort.nullsFirst()/nullsLast() in code shared with MongoDB repositories
- Centralize sort construction per persistence backend
- Test sort expressions against MongoDB in CI if sorts are built dynamically
When it happens
Trigger: Building a Sort that includes Sort.nullsFirst(...) or Sort.nullsLast(...) and passing it to a reactive MongoDB Panache query (find/list/findAll with sort), which calls getSortDocument.
Common situations: Porting Hibernate ORM/SQL Panache code (where nulls first/last is supported) to MongoDB Panache; sharing a common Sort-building utility between relational and MongoDB repositories; copy-pasted sorting logic that includes null-precedence modifiers.
Related errors
- Cannot call a page related method in a ranged query, call pa
- Cannot call a page related method, call page(Page) or page(i
- Cannot call a page related method in a ranged query, call pa
- Cannot sort by nulls first or nulls last
- Update types are only supported in MongoDB contexts
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/a69a315fe2dd22a9.
Report an issue: GitHub.