{"record":{"id":"a67ac32936de62ef","repo":"quarkusio/quarkus","slug":"cannot-sort-by-nulls-first-or-nulls-last-a67ac3","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/runtime/MongoOperations.java","lineNumber":644,"sourceCode":"    }\n\n    public QueryType findAll(Class<?> entityClass, Sort sort) {\n        MongoCollection collection = mongoCollection(entityClass);\n        Bson sortDoc = sortToDocument(sort);\n        ClientSession session = getSession(entityClass);\n        return createQuery(collection, session, 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 List<?> listAll(Class<?> entityClass) {\n        return list(findAll(entityClass));\n    }\n\n    public List<?> listAll(Class<?> entityClass, Sort sort) {\n        return list(findAll(entityClass, sort));\n    }\n\n    public Stream<?> streamAll(Class<?> entityClass) {\n        return stream(findAll(entityClass));\n    }\n\n    public Stream<?> streamAll(Class<?> entityClass, Sort sort) {","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/MongoOperations.java#L626-L662","documentation":"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.","triggerScenarios":"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.).","commonSituations":"Sharing Sort-building code between Hibernate Panache (which supports null precedence) and MongoDB Panache; upgrading code that newly uses nullsFirst/nullsLast.","solutions":["Remove nullsFirst()/nullsLast() from the Sort for MongoDB queries.","Handle null ordering in the query pipeline with an aggregation ($sort + $ifNull or $type-based stages) instead of Panache Sort.","Coalesce the sort field at write time (store a sentinel value) so plain ascending/descending sort suffices."],"exampleFix":"// before\nquery.sort(Sort.by(\"deletedAt\").nullsLast());\n// after\nquery.sort(Sort.by(\"deletedAt\")); // plain asc/desc only in MongoDB","handlingStrategy":"validation","validationCode":"for (Sort.Column c : sort.getColumns()) {\n    if (c.getNullPrecedence() != null) throw new IllegalArgumentException(\"nullsFirst/nullsLast not supported for MongoDB sort: \" + c.getName());\n}","typeGuard":null,"tryCatchPattern":"try {\n    query.sort(sort);\n} catch (UnsupportedOperationException e) {\n    query.sort(Sort.by(/* columns without null precedence */));\n}","preventionTips":["Never use Sort.nullsFirst()/nullsLast() in code paths shared with MongoDB Panache.","Wrap shared sort-building helpers behind a per-backend adapter.","Use aggregation pipelines for explicit null ordering needs."],"tags":["mongodb","panache","sorting","unsupported-feature"],"backgroundTag":"unsupported-null-ordering","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"}