Skip to content

Commit f5e3d9e

Browse files
mongodbenjyemin
andauthored
(DOCSP-29228): Kotlin indexes fundamentals page (#41)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-29228 Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-29228/fundamentals/indexes/ ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working? --------- Co-authored-by: Jeff Yemin <[email protected]>
1 parent 19dc8aa commit f5e3d9e

30 files changed

+766
-161
lines changed

examples/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ dependencies {
2323
implementation("org.slf4j:slf4j-api:1.7.32")
2424
implementation("ch.qos.logback:logback-classic:1.2.6")
2525
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
26-
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.1")
2726
implementation("io.netty:netty-all:4.1.91.Final")
2827
implementation("io.netty:netty-tcnative-boringssl-static:2.0.53.Final:${osdetector.classifier}")
2928
}

examples/src/test/kotlin/ChangeTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import org.junit.jupiter.api.TestInstance
1111
import java.util.*
1212
import kotlin.test.*
1313

14-
15-
16-
1714
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1815
internal class ChangeTest {
1916
// :snippet-start: data-model

examples/src/test/kotlin/DeleteTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import com.mongodb.client.model.Filters
23
import com.mongodb.client.model.Sorts.ascending
34
import com.mongodb.kotlin.client.coroutine.MongoClient
@@ -12,16 +13,15 @@ import org.junit.jupiter.api.TestInstance
1213
import java.util.*
1314
import kotlin.test.*
1415

15-
// :snippet-start: delete-data-model
16-
data class PaintOrder(
17-
@BsonId val id: Int,
18-
val qty: Int,
19-
val color: String
20-
)
21-
// :snippet-end:
22-
2316
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2417
internal class DeleteTest {
18+
// :snippet-start: delete-data-model
19+
data class PaintOrder(
20+
@BsonId val id: Int,
21+
val qty: Int,
22+
val color: String
23+
)
24+
// :snippet-end:
2525

2626
companion object {
2727
val dotenv = dotenv()

examples/src/test/kotlin/IndexesTest.kt

Lines changed: 463 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
val clusteredIndexOptions = ClusteredIndexOptions(Document("_id", 1), true)
2+
val createCollectionOptions = CreateCollectionOptions().clusteredIndexOptions(clusteredIndexOptions)
3+
4+
database.createCollection("vendors", createCollectionOptions)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val filter = Filters.and(
2+
Filters.eq(Movie::type.name, "movie"),
3+
Filters.eq(Movie::rated.name, "G")
4+
)
5+
val sort = Sorts.ascending(Movie::type.name, Movie::rated.name)
6+
val projection = Projections.fields(
7+
Projections.include(Movie::type.name, Movie::rated.name),
8+
Projections.excludeId()
9+
)
10+
val resultsFlow = moviesCollection.find(filter).sort(sort).projection(projection)
11+
resultsFlow.collect { println(it) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val resultCreateIndex = moviesCollection.createIndex(Indexes.ascending(Movie::type.name, Movie::rated.name))
2+
println("Index created: $resultCreateIndex")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
val resultCreateIndex = moviesCollection.createIndex(Indexes.ascending(Movie::type.name, Movie::rated.name))
2+
println("Index created: $resultCreateIndex")
3+
4+
val filter = Filters.and(
5+
Filters.eq(Movie::type.name, "movie"),
6+
Filters.eq(Movie::rated.name, "G")
7+
)
8+
val sort = Sorts.ascending(Movie::type.name, Movie::rated.name)
9+
val projection = Projections.fields(
10+
Projections.include(Movie::type.name, Movie::rated.name),
11+
Projections.excludeId()
12+
)
13+
val resultsFlow = moviesCollection.find(filter).sort(sort).projection(projection)
14+
resultsFlow.collect { println(it) }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Data class for the movies collection
2+
data class Movie(
3+
val title: String,
4+
val year: Int,
5+
val cast: List<String>,
6+
val genres: List<String>,
7+
val type: String,
8+
val rated: String,
9+
val plot: String,
10+
val fullplot: String,
11+
)
12+
13+
// Data class for the theaters collection
14+
data class Theater(
15+
val theaterId: Int,
16+
val location: Location
17+
) {
18+
data class Location(
19+
val address: Address,
20+
val geo: Point
21+
) {
22+
data class Address(
23+
val street1: String,
24+
val city: String,
25+
val state: String,
26+
val zipcode: String
27+
)
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviesCollection.dropIndex("*")

0 commit comments

Comments
 (0)