Browse Source

Add Criteria infix functions for `maxDistance` and `minDistance`.

Closes: #3761
pull/3765/head
sangyongchoi 4 years ago committed by Mark Paluch
parent
commit
302c8031f9
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 22
      spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt
  2. 48
      spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

22
spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt

@ -364,6 +364,28 @@ infix fun KProperty<GeoJson<*>>.maxDistance(d: Double): Criteria =
infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria = infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria =
Criteria(asString(this)).minDistance(d) Criteria(asString(this)).minDistance(d)
/**
* Creates a geo-spatial criterion using a $maxDistance operation, for use with $near
*
* See [MongoDB Query operator:
* $maxDistance](https://docs.mongodb.com/manual/reference/operator/query/maxDistance/)
* @author Sangyong Choi
* @since 3.2
* @see Criteria.maxDistance
*/
infix fun Criteria.maxDistance(d: Double): Criteria =
this.maxDistance(d)
/**
* Creates a geospatial criterion using a $minDistance operation, for use with $near or
* $nearSphere.
* @author Sangyong Choi
* @since 3.2
* @see Criteria.minDistance
*/
infix fun Criteria.minDistance(d: Double): Criteria =
this.minDistance(d)
/** /**
* Creates a criterion using the $elemMatch operator * Creates a criterion using the $elemMatch operator
* *

48
spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

@ -317,6 +317,54 @@ class TypedCriteriaExtensionsTests {
assertThat(typed).isEqualTo(expected) assertThat(typed).isEqualTo(expected)
} }
@Test
fun `maxDistance() should equal expected criteria with nearSphere`() {
val point = Point(0.0, 0.0)
val typed = Building::location nearSphere point maxDistance 3.0
val expected = Criteria("location")
.nearSphere(point)
.maxDistance(3.0)
assertThat(typed).isEqualTo(expected)
}
@Test
fun `minDistance() should equal expected criteria with nearSphere`() {
val point = Point(0.0, 0.0)
val typed = Building::location nearSphere point minDistance 3.0
val expected = Criteria("location")
.nearSphere(point)
.minDistance(3.0)
assertThat(typed).isEqualTo(expected)
}
@Test
fun `maxDistance() should equal expected criteria with near`() {
val point = Point(0.0, 0.0)
val typed = Building::location near point maxDistance 3.0
val expected = Criteria("location")
.near(point)
.maxDistance(3.0)
assertThat(typed).isEqualTo(expected)
}
@Test
fun `minDistance() should equal expected criteria with near`() {
val point = Point(0.0, 0.0)
val typed = Building::location near point minDistance 3.0
val expected = Criteria("location")
.near(point)
.minDistance(3.0)
assertThat(typed).isEqualTo(expected)
}
@Test @Test
fun `elemMatch() should equal expected criteria`() { fun `elemMatch() should equal expected criteria`() {

Loading…
Cancel
Save