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

MongoDB sort documents have no native nulls-first/nulls-last semantics, so when Panache converts a Sort to a MongoDB sort Document it rejects any column with a non-default null precedence by throwing UnsupportedOperationException.

Source

Thrown at extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/MongoOperations.java:644

    }

    public QueryType findAll(Class<?> entityClass, Sort sort) {
        MongoCollection collection = mongoCollection(entityClass);
        Bson sortDoc = sortToDocument(sort);
        ClientSession session = getSession(entityClass);
        return createQuery(collection, session, 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 List<?> listAll(Class<?> entityClass) {
        return list(findAll(entityClass));
    }

    public List<?> listAll(Class<?> entityClass, Sort sort) {
        return list(findAll(entityClass, sort));
    }

    public Stream<?> streamAll(Class<?> entityClass) {
        return stream(findAll(entityClass));
    }

    public Stream<?> streamAll(Class<?> entityClass, Sort sort) {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove nullsFirst()/nullsLast() from the Sort for MongoDB queries.
  2. Handle null ordering in the query pipeline with an aggregation ($sort + $ifNull or $type-based stages) instead of Panache Sort.
  3. Coalesce the sort field at write time (store a sentinel value) so plain ascending/descending sort suffices.

Example fix

// before
query.sort(Sort.by("deletedAt").nullsLast());
// after
query.sort(Sort.by("deletedAt")); // plain asc/desc only in MongoDB
Defensive patterns

Strategy: validation

Validate before calling

for (Sort.Column c : sort.getColumns()) {
    if (c.getNullPrecedence() != null) throw new IllegalArgumentException("nullsFirst/nullsLast not supported for MongoDB sort: " + c.getName());
}

Try / catch

try {
    query.sort(sort);
} catch (UnsupportedOperationException e) {
    query.sort(Sort.by(/* columns without null precedence */));
}

Prevention

When it happens

Trigger: Building a Sort with Sort.by(...).nullsFirst() or .nullsLast() (or a column whose getNullPrecedence() != null) and passing it to a MongoDB Panache query (sort, list, page, etc.).

Common situations: Sharing Sort-building code between Hibernate Panache (which supports null precedence) and MongoDB Panache; upgrading code that newly uses nullsFirst/nullsLast.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/a67ac32936de62ef. Report an issue: GitHub.