Skip to content

Add Criteria infix functions for maxDistance and minDistance #3761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,28 @@ infix fun KProperty<GeoJson<*>>.maxDistance(d: Double): Criteria =
infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria =
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,54 @@ class TypedCriteriaExtensionsTests {
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
fun `elemMatch() should equal expected criteria`() {

Expand Down