Kian
Kian

Reputation: 735

How to make a nearBy-search with lat/lng fields with Spring Data MongoDB

I have two Double-Fields(lat/lng) in my Document-class.

Double lat, lng;

How can I use them with Spring Data for a nearBy-search?

I've found only examples with NearQuery and Query. In the NearQuery examples isn't mentioned, which fields are being used, and the Query only works with double-arrays.

Is there a way to make a nearBy-search work with separated lat, lng fields?

With NearQuery (clat/clng is the center point, rangeKm is the radius in km)

NearQuery.near(clat, clng, Metrics.KILOMETERS).maxDistance(rangeKm);

With Query (location is the lat/lng array, clat/clng is the center point, rangeKm is the radius in km)

criteria.and("location").near(new Point(clat, clng)).maxDistance(rangeKm);

EDIT: I decided to make an Double-Array and search it via a Query. Which is the default unit of measurement (meters, kilometers,...) for maxDistance?

Upvotes: 0

Views: 184

Answers (1)

Christoph Strobl
Christoph Strobl

Reputation: 6726

This not possible. You will have to stick to the way MongoDB expects you to store the data. See the MongoDB Documentation on geospatial-indexes for more detail.

Just use one of the following

Double[] location;      // legacy coordinate pair
Point location;         // legacy coordinate pair
GeoJsonPoint location;  // geojson type point

Sure you could write your own Converter to bring data into the format you need but I guess that would be quite some work compared to just store it in the proper way.

Upvotes: 1

Related Questions