Skip to content

Commit 43de140

Browse files
sangyongchoimp911de
authored andcommitted
Add Criteria infix functions for maxDistance and minDistance.
Closes: #3761
1 parent 15b000e commit 43de140

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Diff for: spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt

+22
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,28 @@ infix fun KProperty<GeoJson<*>>.maxDistance(d: Double): Criteria =
364364
infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria =
365365
Criteria(asString(this)).minDistance(d)
366366

367+
/**
368+
* Creates a geo-spatial criterion using a $maxDistance operation, for use with $near
369+
*
370+
* See [MongoDB Query operator:
371+
* $maxDistance](https://docs.mongodb.com/manual/reference/operator/query/maxDistance/)
372+
* @author Sangyong Choi
373+
* @since 3.2
374+
* @see Criteria.maxDistance
375+
*/
376+
infix fun Criteria.maxDistance(d: Double): Criteria =
377+
this.maxDistance(d)
378+
379+
/**
380+
* Creates a geospatial criterion using a $minDistance operation, for use with $near or
381+
* $nearSphere.
382+
* @author Sangyong Choi
383+
* @since 3.2
384+
* @see Criteria.minDistance
385+
*/
386+
infix fun Criteria.minDistance(d: Double): Criteria =
387+
this.minDistance(d)
388+
367389
/**
368390
* Creates a criterion using the $elemMatch operator
369391
*

Diff for: spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

+48
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,54 @@ class TypedCriteriaExtensionsTests {
317317
assertThat(typed).isEqualTo(expected)
318318
}
319319

320+
@Test
321+
fun `maxDistance() should equal expected criteria with nearSphere`() {
322+
val point = Point(0.0, 0.0)
323+
324+
val typed = Building::location nearSphere point maxDistance 3.0
325+
val expected = Criteria("location")
326+
.nearSphere(point)
327+
.maxDistance(3.0)
328+
329+
assertThat(typed).isEqualTo(expected)
330+
}
331+
332+
@Test
333+
fun `minDistance() should equal expected criteria with nearSphere`() {
334+
val point = Point(0.0, 0.0)
335+
336+
val typed = Building::location nearSphere point minDistance 3.0
337+
val expected = Criteria("location")
338+
.nearSphere(point)
339+
.minDistance(3.0)
340+
341+
assertThat(typed).isEqualTo(expected)
342+
}
343+
344+
@Test
345+
fun `maxDistance() should equal expected criteria with near`() {
346+
val point = Point(0.0, 0.0)
347+
348+
val typed = Building::location near point maxDistance 3.0
349+
val expected = Criteria("location")
350+
.near(point)
351+
.maxDistance(3.0)
352+
353+
assertThat(typed).isEqualTo(expected)
354+
}
355+
356+
@Test
357+
fun `minDistance() should equal expected criteria with near`() {
358+
val point = Point(0.0, 0.0)
359+
360+
val typed = Building::location near point minDistance 3.0
361+
val expected = Criteria("location")
362+
.near(point)
363+
.minDistance(3.0)
364+
365+
assertThat(typed).isEqualTo(expected)
366+
}
367+
320368
@Test
321369
fun `elemMatch() should equal expected criteria`() {
322370

0 commit comments

Comments
 (0)