- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.1k
 
Description
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?