{"record":{"id":"36fe12c5c2f432a3","repo":"quarkusio/quarkus","slug":"there-should-be-no-more-than-one-result-36fe12","errorCode":null,"errorMessage":"There should be no more than one result","messagePattern":"There should be no more than one result","errorType":"exception","errorClass":"PanacheQueryException","httpStatus":null,"severity":"error","filePath":"extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java","lineNumber":234,"sourceCode":"    }\n\n    public <T extends Entity> Optional<T> firstResultOptional() {\n        return Optional.ofNullable(firstResult());\n    }\n\n    public <T extends Entity> T singleResult() {\n        List<T> list = list(2);\n        if (list.size() != 1) {\n            throw new PanacheQueryException(\"There should be only one result\");\n        }\n\n        return list.get(0);\n    }\n\n    public <T extends Entity> Optional<T> singleResultOptional() {\n        List<T> list = list(2);\n        if (list.size() > 1) {\n            throw new PanacheQueryException(\"There should be no more than one result\");\n        }\n\n        return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));\n    }\n\n    private void manageOffsets(FindIterable find, Integer limit) {\n        if (range != null) {\n            find.skip(range.getStartIndex());\n            if (limit == null) {\n                // range is 0 based, so we add 1 to the limit\n                find.limit(range.getLastIndex() - range.getStartIndex() + 1);\n            }\n        } else if (page != null) {\n            find.skip(page.index * page.size);\n            if (limit == null) {\n                find.limit(page.size);\n            }\n        }","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java#L216-L252","documentation":"Thrown by MongoDB Panache's singleResultOptional() when the query fetches two results and finds more than one. The method loads at most 2 documents to distinguish zero, one and many matches; size > 1 means the query is not uniquely selective (e.g., a find on a non-unique field), so returning an Optional would silently hide the ambiguity and the guard aborts instead. Contrast with singleResult(), which also rejects the zero-match case.","triggerScenarios":"Calling query.singleResultOptional() on a query that matches two or more documents.","commonSituations":"Query by non-unique field (e.g. email not indexed as unique); MongoDB data mutated concurrently between design and runtime; missing filter matching all documents.","solutions":["Refine the query filter so it can match at most one document (query by unique _id or a unique field).","Deduplicate the collection data.","If multiple rows are legitimate, use firstResult() or list() instead."],"exampleFix":"// before\nOptional<User> u = PanacheQuery.find(\"lastName\", name).singleResultOptional(); // throws if 2+ users share the name\n// after\nOptional<User> u = PanacheQuery.find(\"email\", email).singleResultOptional(); // query by unique field","handlingStrategy":"try-catch","validationCode":"long count = PanacheEntity.count(\"email\", email);\nif (count > 1) { /* handle duplicates first */ }","typeGuard":null,"tryCatchPattern":"try {\n    Optional<MyEntity> e = query.singleResultOptional();\n} catch (PanacheQueryException e) {\n    // more than one row; use list() and disambiguate\n}","preventionTips":["Only call singleResultOptional() on queries filtered by a unique field.","Add unique indexes in MongoDB and validate at write time.","Use firstResultOptional() when duplicates should just be ignored."],"tags":["mongodb","panache","query-result-count"],"backgroundTag":"query-result-count-mismatch","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}