8000 (DOCSP-29210): CRUD > Search Geospatially page by cbullinger · Pull Request #13 · mongodb/docs-kotlin · GitHub
[go: up one dir, main page]

Skip to content

(DOCSP-29210): CRUD > Search Geospatially page #13

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

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply review feedback
  • Loading branch information
cbullinger committed May 5, 2023
commit dbda66f0da1a466fbf36fad1a9f896bcfb487210
58 changes: 41 additions & 17 deletions examples/src/test/kotlin/GeoTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import com.mongodb.client.model.Filters.geoWithin
import com.mongodb.client.model.Filters.near
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Indexes
import com.mongodb.client.model.Projections
import com.mongodb.client.model.Projections.*
import com.mongodb.client.model.geojson.Point
import com.mongodb.client.model.geojson.Polygon
Expand All @@ -10,10 +10,9 @@ import com.mongodb.kotlin.client.coroutine.MongoClient
import io.github.cdimascio.dotenv.dotenv
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.Test
import java.util.*
import kotlin.test.*

Expand Down Expand Up @@ -319,18 +318,35 @@ internal class SearchGeospatialTest {
@JvmStatic
private fun afterAll() {
runBlocking {
collection.dropIndex("location.geo_2dsphere")
collection.dropIndex("coordinates_2d")

collection.drop()
client.close()

}
}
}

@BeforeEach
fun beforeEach() { runBlocking {
collection.createIndex((Indexes.geo2dsphere("${Theater::location.name}.${Theater.Location::geo.name}")))
collection.createIndex((Indexes.geo2d("coordinates")))
}}

@AfterEach
fun afterEach() {
runBlocking {
collection.dropIndex("location.geo_2dsphere")
collection.dropIndex("coordinates_2d")
}}

@Test
fun indexTest() = runBlocking {
collection.dropIndex("location.geo_2dsphere")
collection.dropIndex("coordinates_2d")
// :snippet-start: geo2dsphere-index
collection.createIndex((Indexes.geo2dsphere("location.geo")))
collection.createIndex((Indexes.geo2dsphere(
"${Theater::location.name}.${Theater.Location::geo.name}"))
)
// :snippet-end:
// :snippet-start: geo2d-index
collection.createIndex((Indexes.geo2d("coordinates")))
Expand All @@ -345,14 +361,19 @@ internal class SearchGeospatialTest {
val database = client.getDatabase("sample_mflix")
val collection = database.getCollection<TheaterResults>("theaters")
val centralPark = Point(Position(-73.9667, 40.78))
val query = near("location.geo", centralPark, 10000.0, 5000.0)
val projection = fields(include("location.address.city"), excludeId())
val resultsFlow = collection.find(query)
.projection(projection)
val query = Filters.near(
"${Theater::location.name}.${Theater.Location::geo.name}", centralPark, 10000.0, 5000.0
)
val projection = Projections.fields(
Projections.include(
"${Theater::location.name}.${Theater.Location::address.name}.${Theater.Location.Address::city.name}"),
Projections.excludeId()
)
val resultsFlow = collection.find(query).projection(projection)
resultsFlow.collect { println(it) }
// :snippet-end:
val results = resultsFlow.toList()
assertTrue(results.any() { it.location.address.city == "Bronx" })
assertTrue(results.any { it.location.address.city == "Bronx" })
}

@Test
Expand All @@ -366,11 +387,14 @@ internal class SearchGeospatialTest {
Position(-72.0, 40.0)
)
)
val projection = fields(
include("location.address.city"),
excludeId()
val projection = Projections.fields(
Projections.include(
"${Theater::location.name}.${Theater.Location::address.name}.${Theater.Location.Address::city.name}"),
Projections.excludeId()
)
val geoWithinComparison = Filters.geoWithin(
"${Theater::location.name}.${Theater.Location::geo.name}", longIslandTriangle
)
val geoWithinComparison = geoWithin("location.geo", longIslandTriangle)
val resultsFlow = collection.find<TheaterResults>(geoWithinComparison)
.projection(projection)
resultsFlow.collect { println(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
collection.createIndex((Indexes.geo2dsphere("location.geo")))
collection.createIndex((Indexes.geo2dsphere(
"${Theater::location.name}.${Theater.Location::geo.name}"))
)
13 changes: 9 additions & 4 deletions source/examples/generated/GeoTest.snippet.proximity-query.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
val database = client.getDatabase("sample_mflix")
val collection = database.getCollection<TheaterResults>("theaters")
val centralPark = Point(Position(-73.9667, 40.78))
val query = near("location.geo", centralPark, 10000.0, 5000.0)
val projection = fields(include("location.address.city"), excludeId())
val resultsFlow = collection.find(query)
.projection(projection)
val query = Filters.near(
"${Theater::location.name}.${Theater.Location::geo.name}", centralPark, 10000.0, 5000.0
)
val projection = Projections.fields(
Projections.include(
"${Theater::location.name}.${Theater.Location::address.name}.${Theater.Location.Address::city.name}"),
Projections.excludeId()
)
val resultsFlow = collection.find(query).projection(projection)
resultsFlow.collect { println(it) }
11 changes: 7 additions & 4 deletions source/examples/generated/GeoTest.snippet.query-range.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ val longIslandTriangle = Polygon(
Position(-72.0, 40.0)
)
)
val projection = fields(
include("location.address.city"),
excludeId()
val projection = Projections.fields(
Projections.include(
"${Theater::location.name}.${Theater.Location::address.name}.${Theater.Location.Address::city.name}"),
Projections.excludeId()
)
val geoWithinComparison = Filters.geoWithin(
"${Theater::location.name}.${Theater.Location::geo.name}", longIslandTriangle
)
val geoWithinComparison = geoWithin("location.geo", longIslandTriangle)
val resultsFlow = collection.find<TheaterResults>(geoWithinComparison)
.projection(projection)
resultsFlow.collect { println(it) }
2 changes: 2 additions & 0 deletions source/fundamentals/crud/read-operations/geo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ The examples require the following imports:
The data is modeled using the following Kotlin data class:

.. literalinclude:: /examples/generated/GeoTest.snippet.theater-data-class.kt
:language: kotlin

The results are modeled using the following Kotlin data class:

.. literalinclude:: /examples/generated/GeoTest.snippet.results-data-class.kt
:language: kotlin

Query by Proximity
~~~~~~~~~~~~~~~~~~
Expand Down
0