Skip to content

Commit 34329bc

Browse files
DATAMONGO-1110 - Add support for $minDistance for GeoJSON types.
We now make sure $minDistance operator gets correctly nested when using GeoJSON types. Original pull request: #277.
1 parent 91531c3 commit 34329bc

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ public Criteria nearSphere(Point point) {
441441
*/
442442
public Criteria maxDistance(double maxDistance) {
443443

444-
if (createNearCriteriaForCommand("$near", maxDistance) || createNearCriteriaForCommand("$nearSphere", maxDistance)) {
444+
if (createNearCriteriaForCommand("$near", "$maxDistance", maxDistance)
445+
|| createNearCriteriaForCommand("$nearSphere", "$maxDistance", maxDistance)) {
445446
return this;
446447
}
447448

@@ -458,6 +459,12 @@ public Criteria maxDistance(double maxDistance) {
458459
* @since 1.7
459460
*/
460461
public Criteria minDistance(double minDistance) {
462+
463+
if (createNearCriteriaForCommand("$near", "$minDistance", minDistance)
464+
|| createNearCriteriaForCommand("$nearSphere", "$minDistance", minDistance)) {
465+
return this;
466+
}
467+
461468
criteria.put("$minDistance", minDistance);
462469
return this;
463470
}
@@ -618,7 +625,7 @@ private void setValue(DBObject dbo, String key, Object value) {
618625
}
619626
}
620627

621-
private boolean createNearCriteriaForCommand(String command, double maxDistance) {
628+
private boolean createNearCriteriaForCommand(String command, String operation, double maxDistance) {
622629

623630
if (!criteria.containsKey(command)) {
624631
return false;
@@ -628,14 +635,13 @@ private boolean createNearCriteriaForCommand(String command, double maxDistance)
628635

629636
if (existingNearOperationValue instanceof DBObject) {
630637

631-
((DBObject) existingNearOperationValue).put("$maxDistance", maxDistance);
638+
((DBObject) existingNearOperationValue).put(operation, maxDistance);
632639

633640
return true;
634641

635642
} else if (existingNearOperationValue instanceof GeoJson) {
636643

637-
BasicDBObject dbo = new BasicDBObject("$geometry", existingNearOperationValue)
638-
.append("$maxDistance", maxDistance);
644+
BasicDBObject dbo = new BasicDBObject("$geometry", existingNearOperationValue).append(operation, maxDistance);
639645
criteria.put(command, dbo);
640646

641647
return true;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,45 @@ public void shouleSaveAndRetrieveDocumentWithGeoJsonGeometryCollectionTypeCorrec
278278
assertThat(result.geoJsonGeometryCollection, equalTo(obj.geoJsonGeometryCollection));
279279
}
280280

281+
/**
282+
* @see DATAMONGO-1110
283+
*/
284+
@Test
285+
public void nearWithMinDistance() {
286+
287+
Point point = new GeoJsonPoint(-73.99171, 40.738868);
288+
List<Venue2DSphere> venues = template.find(query(where("location").near(point).minDistance(0.01)),
289+
Venue2DSphere.class);
290+
291+
assertThat(venues.size(), is(11));
292+
}
293+
294+
/**
295+
* @see DATAMONGO-1110
296+
*/
297+
@Test
298+
public void nearSphereWithMinDistance() {
299+
300+
Point point = new GeoJsonPoint(-73.99171, 40.738868);
301+
List<Venue2DSphere> venues = template.find(query(where("location").nearSphere(point).minDistance(0.01)),
302+
Venue2DSphere.class);
303+
304+
assertThat(venues.size(), is(11));
305+
}
306+
307+
/**
308+
* @see DATAMONGO-1135
309+
*/
310+
@Test
311+
public void nearWithMinAndMaxDistance() {
312+
313+
GeoJsonPoint point = new GeoJsonPoint(-73.99171, 40.738868);
314+
315+
Query query = query(where("location").near(point).minDistance(0.01).maxDistance(100));
316+
List<Venue2DSphere> venues = template.find(query, Venue2DSphere.class);
317+
assertThat(venues.size(), is(2));
318+
}
319+
281320
private void addVenues() {
282321

283322
template.insert(new Venue2DSphere("Penn Station", -73.99408, 40.75057));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaTests.java

+36
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,40 @@ public void maxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonTyp
211211

212212
assertThat(dbo, IsBsonObject.isBsonObject().containing("foo.$nearSphere.$maxDistance", 50D));
213213
}
214+
215+
/**
216+
* @see DATAMONGO-1110
217+
*/
218+
@Test
219+
public void minDistanceShouldBeMappedInsideNearWhenUsedAlongWithGeoJsonType() {
220+
221+
DBObject dbo = new Criteria("foo").near(new GeoJsonPoint(100, 200)).minDistance(50D).getCriteriaObject();
222+
223+
assertThat(dbo, IsBsonObject.isBsonObject().containing("foo.$near.$minDistance", 50D));
224+
}
225+
226+
/**
227+
* @see DATAMONGO-1110
228+
*/
229+
@Test
230+
public void minDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType() {
231+
232+
DBObject dbo = new Criteria("foo").nearSphere(new GeoJsonPoint(100, 200)).minDistance(50D).getCriteriaObject();
233+
234+
assertThat(dbo, IsBsonObject.isBsonObject().containing("foo.$nearSphere.$minDistance", 50D));
235+
}
236+
237+
/**
238+
* @see DATAMONGO-1110
239+
*/
240+
@Test
241+
public void minAndMaxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType() {
242+
243+
DBObject dbo = new Criteria("foo").nearSphere(new GeoJsonPoint(100, 200)).minDistance(50D).maxDistance(100D)
244+
.getCriteriaObject();
245+
246+
assertThat(dbo, IsBsonObject.isBsonObject().containing("foo.$nearSphere.$minDistance", 50D));
247+
assertThat(dbo, IsBsonObject.isBsonObject().containing("foo.$nearSphere.$maxDistance", 100D));
248+
}
249+
214250
}

0 commit comments

Comments
 (0)