{"record":{"id":"488785466d182969","repo":"quarkusio/quarkus","slug":"there-should-be-no-more-than-one-result","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/reactive/runtime/CommonReactivePanacheQueryImpl.java","lineNumber":230,"sourceCode":"                ? collection.find(Panache.getCurrentSession(), getQuery(), options)\n                : collection.find(getQuery(), options);\n        return results.collect().asList().map(list -> {\n            if (list.size() != 1) {\n                throw new PanacheQueryException(\"There should be only one result\");\n            } else {\n                return list.get(0);\n            }\n        });\n    }\n\n    public <T extends Entity> Uni<Optional<T>> singleResultOptional() {\n        FindOptions options = buildOptions(2);\n        Multi<T> results = Panache.getCurrentSession() != null\n                ? collection.find(Panache.getCurrentSession(), getQuery(), options)\n                : collection.find(getQuery(), options);\n        return results.collect().asList().map(list -> {\n            if (list.size() == 2) {\n                throw new PanacheQueryException(\"There should be no more than one result\");\n            }\n            return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));\n        });\n    }\n\n    private FindOptions buildOptions() {\n        FindOptions options = new FindOptions();\n        options.sort(sort);\n        if (range != null) {\n            // range is 0 based, so we add 1 to the limit\n            options.skip(range.getStartIndex()).limit(range.getLastIndex() - range.getStartIndex() + 1);\n        } else if (page != null) {\n            options.skip(page.index * page.size).limit(page.size);\n        }\n        if (projections != null) {\n            options.projection(this.projections);\n        }\n        if (this.collation != null) {","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/CommonReactivePanacheQueryImpl.java#L212-L248","documentation":"singleResultOptional() fetches up to 2 rows and throws PanacheQueryException when the query matches more than one document (list.size() == 2). Unlike singleResult(), an empty result is acceptable (returns Optional.empty()), but duplicate matches still violate the 'at most one' contract.","triggerScenarios":"Calling singleResultOptional() on a query whose filter matches 2+ documents; this happens when the 'unique' field has duplicate values because MongoDB enforces no uniqueness unless an index is created.","commonSituations":"Duplicate records inserted before a unique index was added; case-sensitive duplicates ('Foo@x.com' vs 'foo@x.com'); soft-deleted copies of the same logical entity still present in the collection.","solutions":["Create a unique index on the lookup field and clean up existing duplicates","Narrow the query filter (e.g. add tenant or status = ACTIVE) so only one document matches","Deduplicate data before deploying the field as a logical unique key"],"exampleFix":"// before\nOptional<User> u = reactiveFind(\"email\", email).singleResultOptional(); // throws on dupes\n\n// after\ndb.users.createIndex({ email: 1 }, { unique: true, collation: { locale: 'en', strength: 2 } });\nOptional<User> u = reactiveFind(\"email\", email).singleResultOptional();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return query.singleResultOptional().await().indefinitely();\n} catch (PanacheQueryException e) {\n    // duplicates found: log entity/field and either deduplicate or use firstResult()\n    return Optional.empty();\n}","preventionTips":["Back logical unique keys with MongoDB unique indexes","Clean up duplicates before adding single-result expectations","Consider firstResultOptional() if duplicates are tolerated in your data model"],"tags":["panache","mongodb","query-result","unique-constraint"],"backgroundTag":"unexpected-result-count","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"}