quarkusio/quarkus · error · PanacheQueryException
There should be no more than one result
Error message
There should be no more than one result
What it means
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.
Source
Thrown at extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java:234
}
public <T extends Entity> Optional<T> firstResultOptional() {
return Optional.ofNullable(firstResult());
}
public <T extends Entity> T singleResult() {
List<T> list = list(2);
if (list.size() != 1) {
throw new PanacheQueryException("There should be only one result");
}
return list.get(0);
}
public <T extends Entity> Optional<T> singleResultOptional() {
List<T> list = list(2);
if (list.size() > 1) {
throw new PanacheQueryException("There should be no more than one result");
}
return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
}
private void manageOffsets(FindIterable find, Integer limit) {
if (range != null) {
find.skip(range.getStartIndex());
if (limit == null) {
// range is 0 based, so we add 1 to the limit
find.limit(range.getLastIndex() - range.getStartIndex() + 1);
}
} else if (page != null) {
find.skip(page.index * page.size);
if (limit == null) {
find.limit(page.size);
}
}View on GitHub (pinned to e1c734241f)
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.
Example fix
// before
Optional<User> u = PanacheQuery.find("lastName", name).singleResultOptional(); // throws if 2+ users share the name
// after
Optional<User> u = PanacheQuery.find("email", email).singleResultOptional(); // query by unique field Defensive patterns
Strategy: try-catch
Validate before calling
long count = PanacheEntity.count("email", email);
if (count > 1) { /* handle duplicates first */ } Try / catch
try {
Optional<MyEntity> e = query.singleResultOptional();
} catch (PanacheQueryException e) {
// more than one row; use list() and disambiguate
} Prevention
- 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.
When it happens
Trigger: Calling query.singleResultOptional() on a query that matches two or more documents.
Common situations: 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.
Related errors
- There should be only one result
- Cannot call a page related method in a ranged query, call pa
- There should be only one result
- There should be no more than one result
- Illegal read preference configured in the @MongoEntity anno
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/36fe12c5c2f432a3.
Report an issue: GitHub.