Skip to content
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

Point must only contain numeric elements when using near count query with GeoJson points #4004

Closed
sebastian-firsching opened this issue Mar 22, 2022 · 4 comments
Assignees
Labels
type: bug A general bug

Comments

@sebastian-firsching
Copy link

Count queries using $near or $nearSphere require a rewrite to $geoWithin. However, according to the official MongoDB documenatation, the syntax for a $near query is different when using a GeoJSON point instead of legacy coordinates.
When creating the $geoWithin in CountQuery.createGeoWithin in combination with a GeoJSON point, the $maxDistance cannot be found, as it is nested inside the $near document. With the current implementation, $maxDistance is only detected if it is on the source level (which is the case when legacy coordinates are used):

boolean spheric = source.containsKey("$nearSphere");
Object $near = spheric ? source.get("$nearSphere") : source.get("$near");

Number maxDistance = source.containsKey("$maxDistance") ? (Number) source.get("$maxDistance") : Double.MAX_VALUE;

This results in maxDistance having the value Double.MAX_VALUE (which is 1.7976931348623157E308). When executing the rewritten query against MongoDB, this fails with the error org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 2 (BadValue): 'Point must only contain numeric elements' on server localhost:27017, as it cannot handle the scientific notation (1.7976931348623157E308) of Double.MAX_VALUE.
Could someone help with that issue?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 22, 2022
@christophstrobl
Copy link
Member

Thanks for reaching out could you please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@christophstrobl christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Mar 22, 2022
@sebastian-firsching
Copy link
Author

Sure, I have created a quick example that executes a count with a near query and GeoJSON point in an application runner directly after startup. Just make sure, that you have a MongoDB running locally on port 27017.
geojson-near-example.zip

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 22, 2022
@christophstrobl
Copy link
Member

Thanks for the reproducer - the rewrite of $near and $nearSphere is missing specific handling for geoJson $geometry which needs to be converted into a legacy coordinate pair for $geowithin $center | $centerSphere.
As noted the $maxDistance also is in a different place and needs to extracted from the given source. A bit of extra complexity comes in by $nearSphere using meters in case of geoJson, whereas $centerSphere operates upon radians.

@christophstrobl
Copy link
Member

Related to: #2925

@christophstrobl christophstrobl self-assigned this Mar 23, 2022
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 23, 2022
@mp911de mp911de added this to the 3.4.4 (2021.2.4) milestone Sep 21, 2022
@mp911de mp911de changed the title Doing a count with a near query and GeoJson point Enable near count query using GeoJson points Sep 21, 2022
@mp911de mp911de changed the title Enable near count query using GeoJson points Point must only contain numeric elements when using near count query with GeoJson points Sep 21, 2022
mp911de pushed a commit that referenced this issue Sep 21, 2022
Update Javadoc to mention unit of measure for min/maxDistance depending on usage of geoJson.
Also remove unused imports from tests

See #4004
Original pull request: #4006.
mp911de added a commit that referenced this issue Sep 21, 2022
Reformat code.

See #4004
Original pull request: #4006.
mp911de pushed a commit that referenced this issue Sep 21, 2022
$near and $nearSphere queries are not supported via countDocuments and the used aggregation match stage and need to be rewritten to $geoWithin. The existing logic did not cover usage of geoJson types, which is fixed now. In case of nearSphere it is also required to convert the $maxDistance argument (given in meters for geoJson) to radians which is used by $geoWithin $centerSphere.

Closes #4004
Original pull request: #4006.
Related to #2925
mp911de pushed a commit that referenced this issue Sep 21, 2022
Update Javadoc to mention unit of measure for min/maxDistance depending on usage of geoJson.
Also remove unused imports from tests

See #4004
Original pull request: #4006.
mp911de added a commit that referenced this issue Sep 21, 2022
Reformat code.

See #4004
Original pull request: #4006.
@mp911de mp911de removed the status: feedback-provided Feedback has been provided label Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants