Using the latest Spring Data Mongo (2.1.1 at time of writing), how do I specify to get the first record of a "custom" query method? Here is an example:
@Query(value="{name: ?0, approval: {'$ne': null}}",
sort="{'approval.approvedDate': -1}",
fields = "{ _id: 1 }")
List<Item> getLatestApprovedIdByName(String name, Pageable pageable);
/**
* Finds the id of the most recently approved document with the given name.
*/
default Item getLatestApprovedIdByName(String name) {
return getLatestApprovedIdByName(name, PageRequest.of(0, 1)).stream()
.findFirst()
.orElse(null);
}
Ideally I could just annotate getLatestApprvedIdByName taking only the String
parameter.
There doesn't seem to be a limit field on the org.springframework.data.mongodb.repository.Query
annotation.
It seems odd because I can emulate everything the named methods do except findFirst
.
Without the Pageable
, I get IncorrectResultSizeDataAccessException
, and returning a List
is not acceptable because I don't want to waste time returning an arbitrarily large result, plus the complicated code needing to deal with the possibility of 0 or 1 items.