-
Notifications
You must be signed in to change notification settings - Fork 1.1k
String to ObjectId mapping error when using query method #4490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I dont know if you have any particular reason to use "findAll", but if you call "findByIdGreaterThan" (return Optional) it will do what you describe. Btw, ID's should be unique, so always it will be found 1 or 0, and not a List. PS.: On your custom query you use GTE and your jpa is on GT. |
I need to use "findAll" by my business logic requirement, so "findByIdGreaterThan" is not appropriate choice. Also, "findByIdGreaterThan" still queries by string type _id. |
Can you describe better your case? Because I can't see where it could be appropriate. I can imagine only one scenario when you have compound id, like:
Your document will be:
About "findByIdGreaterThan", you should use ObjectId in your method on interface, like
It will produce same @query that you describe on your custom |
I need to get list of documents greater than certain _id, for pagination. _id is used as cursor, and I need range query. |
It works well if I declare id with ObjectId. If I use String type id and call So I think there's hole regarding id field conversion in query method. |
The generated query should take the |
@christophstrobl Here's sample git repository. |
@christophstrobl |
@grwang91 it's on the list of issues to tackle. If you've time to investigate further and propose a solution we'd be happy to review it. |
@christophstrobl First option is modifying at MongoQueryCreator level. (Submitted PR) As a maintainer, can you advise which option is better? |
Thank you for digging into it. The |
Having problem using query method, as query generated by spring-data-mongodb is consist of string type _id, not ObjectId.
Entity and Repository are declared like above code.
And I tested by below simple test code.
It is supposed to return all documents in DB, as "000000000000000000000000" is minimum value of ObjectId. But nothing returned.
Also, It seems Mongotemplate is querying by string type _id.
I could solve this problem with a temporary solution, by using custom @query like below, with appropriate debug log using $oid.
But still I can't use query method with _id.
Am I using query method in wrong way? or It's id type bug?
The text was updated successfully, but these errors were encountered: