{"record":{"id":"a69a315fe2dd22a9","repo":"quarkusio/quarkus","slug":"cannot-sort-by-nulls-first-or-nulls-last","errorCode":null,"errorMessage":"Cannot sort by nulls first or nulls last","messagePattern":"Cannot sort by nulls first or nulls last","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/ReactiveMongoOperations.java","lineNumber":609,"sourceCode":"        return createQuery(collection, null, null);\n    }\n\n    public QueryType findAll(Class<?> entityClass, Sort sort) {\n        ReactiveMongoCollection collection = mongoCollection(entityClass);\n        Bson sortDoc = sortToDocument(sort);\n        return createQuery(collection, null, sortDoc);\n    }\n\n    private Bson sortToDocument(Sort sort) {\n        if (sort == null) {\n            return null;\n        }\n\n        Document sortDoc = new Document();\n        for (Sort.Column col : sort.getColumns()) {\n            sortDoc.append(col.getName(), col.getDirection() == Sort.Direction.Ascending ? 1 : -1);\n            if (col.getNullPrecedence() != null) {\n                throw new UnsupportedOperationException(\"Cannot sort by nulls first or nulls last\");\n            }\n        }\n        return sortDoc;\n    }\n\n    public Uni<List<?>> listAll(Class<?> entityClass) {\n        return (Uni) list(findAll(entityClass));\n    }\n\n    public Uni<List<?>> listAll(Class<?> entityClass, Sort sort) {\n        return (Uni) list(findAll(entityClass, sort));\n    }\n\n    public Multi<?> streamAll(Class<?> entityClass) {\n        return stream(findAll(entityClass));\n    }\n\n    public Multi<?> streamAll(Class<?> entityClass, Sort sort) {","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/ReactiveMongoOperations.java#L591-L627","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nSort sort = Sort.descending(\"deletedAt\").nullsLast(\"deletedAt\"); // throws\n\n// after\nSort sort = Sort.descending(\"deletedAt\"); // MongoDB default null ordering","handlingStrategy":"validation","validationCode":"boolean hasNullPrecedence = sort.getColumns().stream().anyMatch(c -> c.getNullPrecedence() != null);\nif (hasNullPrecedence) throw new IllegalArgumentException(\"nullsFirst/nullsLast not supported by MongoDB Panache\");","typeGuard":null,"tryCatchPattern":"try {\n    return repository.list(sort);\n} catch (UnsupportedOperationException e) {\n    return repository.list(sortIgnoringNullPrecedence(sort));\n}","preventionTips":["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"],"tags":["panache","mongodb","sorting","unsupported-operation"],"backgroundTag":"unsupported-sort-option","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}