Unable to replicate .skip(...)
within Mongo repository method using @Query
annotation?
#4573
Labels
for: team-attention
An issue we need to discuss as a team to make progress
status: feedback-provided
Feedback has been provided
status: waiting-for-triage
An issue we've not yet triaged
Hi, we have some APIs on our server that allow the client to specify a custom
skip
andlimit
value for returning back the exact segment of results they want to see. We are using the Spring Data Mongo repository framework for implementing the queries on the backend. After a bunch of research, it doesn't seem possible to make the customskip
work with Spring repositories using the@Query
annotation.Pageable
construct, but this forces the slice of data that's returned to always be a multiple of the page size that's specified. Thus, if my page size is50
but I want to return results from number 30-80, there is no way to be able to accomplish this straightforwardly usingPageRequest
s - I would be forced to get 2 pages of data and then splice together the actual data the user is requesting.$skip
operator, but Mongo already allows skipping on normal find queries without needing any aggregation pipeline.query.skip(...)
in Java code. However, this is precisely what I want to avoid since the@Query
annotation is already 95% the way there in generating this query declaratively.I think the optimal solution to make the
skip
option in Mongo workable with the Mongo repository@Query
framework is to add a new fieldinitialOffset
inAbstractPageRequest.java
. That initialOffset would be between[0, pageSize)
, and it would get added on in thepublic long getOffset()
calculation to decide the final offset. (In my example above, initialOffset would be 30, so withpageSize=50
, page 0 would cover elements 30-79.)If modifying
AbstractPageRequest
isn't possible, then perhaps we could somehow add askip
parameter to the@Query
annotation and apply that to the query before thePageable
offset is added on. I did some poking around in the spring-data-mongodb codebase, and it seems thatprotected Object doExecute
within AbstractMongoQuery.java does the magic for converting the@Query
annotation into a normal SpringQuery()
object:In addition, MongoQueryExecution.PagedExecution sets the skip on the query before the final execution. I'm not sure yet whether something can be hooked up between these, but it would only matter if we can't modify
AbstractPageRequest
.Hopefully everything I said above made sense. Assuming this could be approved, I think I could work on a PR for this in the near future if the Spring team is too busy with other stuff.
Note: I saw this older issue "Add skip, limit and sort query feature to
@Query
annotation", but there is no solution in there to accomplish what the.skip()
method does in normal Java code.The text was updated successfully, but these errors were encountered: