Skip to content

nearSphere query wrongly generated with radian parameter instead of meters [DATAMONGO-2394] #3254

Closed
@spring-projects-issues

Description

@spring-projects-issues

gagan405 opened DATAMONGO-2394 and commented

As per the Mongo DB documentation here: https://docs.mongodb.com/manual/reference/operator/query/nearSphere/

The nearSphere query expects maxDistance in meters. And in legacy mode, it expects in radians.

However, spring-data-mongo generates non-legacy query with radian value in maxDistance.

The linked github gist has more details, but to summarize here:

Repository code:

Collection<House> findByLocationNear(GeoJsonPoint center, Distance distance);

Document:

@Document
public class House {

    @Id
    private String id;

    @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
    GeoJsonPoint location;

    private Person owner;
    private Address address;
}

Invoke

Collection<House> houses = repository.findByLocationNear(new GeoJsonPoint(-122.125517, 47.645491d), new Distance(1, Metrics.KILOMETERS));

Generated query:

{ "location" : { "$nearSphere" : { "$geometry" : { "type" : "Point", "coordinates" : [-122.125517, 47.645491] }, "$maxDistance" : 1.567855942887398E-4 } } }

As seen, 1 kilometer is converted to radians, but the query is in GeoJSON syntax.

The correct query should have kept maxDistance to 1000; or should have generated in the legacy format.

 

 


Affects: 2.1.11 (Lovelace SR11)

Reference URL: https://gist.github.com/gagan405/7d0f18e2595e2e60f096b890c98f872c

Issue Links:

  • DATAMONGO-1348 NearQuery uses "legacy coordinate pairs" not GeoJSON points

Referenced from: pull request #798

Backported to: 2.2.1 (Moore SR1)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions