Skip to content

Commit

Permalink
PR restructure and pushing v5.3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Jan 14, 2025
1 parent 6050c93 commit 508d1ca
Show file tree
Hide file tree
Showing 22 changed files with 634 additions and 98 deletions.
2 changes: 1 addition & 1 deletion config/redirects
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define: prefix docs/drivers/kotlin/coroutine
define: base https://www.mongodb.com/${prefix}
define: versions v4.10 v4.11 v5.0 v5.1 v5.2 master
define: versions v4.10 v4.11 v5.0 v5.1 v5.2 v5.3 master

raw: ${prefix}/ -> ${base}/current/

Expand Down
11 changes: 6 additions & 5 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
val kotlin_mongodb_version: String by project

plugins {
kotlin("jvm") version "1.8.0"
id("org.jetbrains.kotlin.jvm") version "1.9.25"
id("com.google.osdetector") version "1.7.3"
application
kotlin("plugin.serialization") version "1.8.21"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.25"
}

group = "org.mongodb.docs.kotlin"
Expand All @@ -26,10 +26,11 @@ dependencies {
implementation("io.netty:netty-tcnative-boringssl-static:2.0.59.Final:${osdetector.classifier}")
implementation("org.xerial.snappy:snappy-java:1.1.10.0")
implementation("com.github.luben:zstd-jni:1.5.5-4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation("org.mongodb:bson-kotlinx:$kotlin_mongodb_version")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
implementation("org.mongodb:mongodb-driver-kotlin-extensions:$kotlin_mongodb_version")
}

tasks.test {
Expand Down
2 changes: 1 addition & 1 deletion examples/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
kotlin_mongodb_version=5.2.0
kotlin_mongodb_version=5.3.0
256 changes: 256 additions & 0 deletions examples/src/test/kotlin/BuildersDataClassTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
import com.mongodb.kotlin.client.model.Aggregates.count
import com.mongodb.client.model.Aggregates.group
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.sort
import com.mongodb.kotlin.client.coroutine.MongoClient
import config.getConfig
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

import com.mongodb.kotlin.client.model.Filters.eq
import com.mongodb.kotlin.client.model.Filters.all
import com.mongodb.kotlin.client.model.Indexes
import com.mongodb.kotlin.client.model.Projections.excludeId
import com.mongodb.kotlin.client.model.Projections.fields
import com.mongodb.kotlin.client.model.Projections.include
import com.mongodb.client.model.Sorts.orderBy
import com.mongodb.kotlin.client.model.Accumulators.avg
import com.mongodb.kotlin.client.model.Sorts

import com.mongodb.kotlin.client.model.Filters.gte
import com.mongodb.kotlin.client.model.Updates.addToSet
import com.mongodb.kotlin.client.model.Updates.combine
import com.mongodb.kotlin.client.model.Updates.max
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class BuildersDataClassTest {

companion object {
val config = getConfig()
val client = MongoClient.create(config.connectionUri)
val database = client.getDatabase("school")

@AfterAll
@JvmStatic
fun afterAll() {
runBlocking {
client.close()
}
}
}

@AfterEach
fun afterEach() {
runBlocking {
database.drop()
}
}

// :snippet-start: data-class
data class Student(
val name: String,
val teachers: List<String>,
val gradeAverage: Double
)
// :snippet-end:


@Test
fun filtersTest() = runBlocking {

val collection = database.getCollection<Student>("students")

// :snippet-start: filters-data-class
val student = Student(
"Sandra Nook",
listOf("Alvarez", "Gruber"),
85.7
)

// Equivalent equality queries
Student::name.eq(student.name)
eq(Student::name, student.name)
Student::name eq student.name // Infix notation

// Equivalent array queries
all(Student::teachers, student.teachers)
Student::teachers.all(student.teachers)
Student::teachers all student.teachers // Infix notation
// :snippet-end:

collection.insertOne(student)
val filter = eq(Student::name, student.name)
val result = collection.find(filter).firstOrNull()
Assertions.assertEquals(student, result)
}

@Test
fun indexesTest() = runBlocking {

val collection = database.getCollection<Student>("students")

// :snippet-start: indexes-data-class
val ascendingIdx = Indexes.ascending(Student::name)
val descendingIdx = Indexes.descending(Student::teachers)

val ascIdxName = collection.createIndex(ascendingIdx)
val descIdxName = collection.createIndex(descendingIdx)
// :snippet-end:

assertEquals("name_1", ascIdxName)
}

@Test
fun projectionsTest() = runBlocking {

val collection = database.getCollection<Student>("students")

val student = Student(
"Sandra Nook",
listOf("Alvarez", "Gruber"),
85.7
)
collection.insertOne(student)

// :snippet-start: projections-data-class
val combinedProj = fields(
include(Student::name, Student::gradeAverage),
excludeId()
)

collection.find().projection(combinedProj)
// :snippet-end:

data class Result(val name: String, val gradeAverage: Double)
val result = collection.find<Result>().projection(combinedProj).firstOrNull()

if (result != null) {
assertEquals(85.7, result.gradeAverage)
}
}

@Test
fun sortsTest() = runBlocking {

val collection = database.getCollection<Student>("students")

val student1 = Student(
"Sandra Nook",
listOf("Alvarez", "Gruber"),
85.7
)
val student2 = Student(
"Paolo Sanchez",
listOf("Gruber", "Piselli"),
89.3
)
collection.insertMany(listOf(student1, student2))

// :snippet-start: sorts-data-class
val sort = orderBy(
Sorts.descending(Student::gradeAverage),
Sorts.ascending(Student::name)
)

collection.find().sort(sort)
// :snippet-end:

val result = collection.find().sort(sort).firstOrNull()

if (result != null) {
assertEquals(89.3, result.gradeAverage)
}
}

@Test
fun updatesTest() = runBlocking {

val collection = database.getCollection<Student>("students")

val students = listOf(
Student("Sandra Nook", listOf("Alvarez", "Gruber"),85.7),
Student("Paolo Sanchez", listOf("Gruber", "Piselli"),89.3)
)
collection.insertMany(students)

// :snippet-start: updates-data-class
val filter = Student::gradeAverage gte 85.0
val update = combine(
addToSet(Student::teachers, "Soto"),
Student::gradeAverage.max(90.0)
)
collection.updateMany(filter, update)
// :snippet-end:

val result = collection.find().firstOrNull()

if (result != null) {
assertTrue("Soto" in result.teachers)
assertEquals(result.gradeAverage, 90.0)
}
}

@Test
fun aggregatesTest() = runBlocking {

val collection = database.getCollection<Student>("students")

val students = listOf(
Student("Sandra Nook", listOf("Alvarez", "Gruber"),85.7),
Student("Paolo Sanchez", listOf("Gruber", "Piselli"),89.3),
Student("Katerina Jakobsen", listOf("Alvarez", "Ender"),97.3),
Student("Emma Frank", listOf("Piselli", "Harbour"),93.4),
Student("Qasim Haq", listOf("Gruber", "Harbour"),80.6)
)
collection.insertMany(students)

// :snippet-start: aggregates-data-class
// Data class to store aggregation result
data class Summary ( val average: Double )

val pipeline = listOf(
// Sorts grades from high to low
sort(Sorts.descending(Student::gradeAverage)),
// Selects the top 3 students
limit(3),
// Calculates the average of their grades and stores value in a Summary instance
group(null, avg(Summary::average, "\$${Student::gradeAverage.name}"))
)

val result = collection.aggregate<Summary>(pipeline)
// :snippet-end:

val r = result.firstOrNull()
if (r != null) {
assertEquals(93.33333333333333, r.average)
}
}

@Test
fun aggregatesCountTest() = runBlocking {

val collection = database.getCollection<Student>("students")

val students = listOf(
Student("Sandra Nook", listOf("Alvarez", "Gruber"),85.7),
Student("Paolo Sanchez", listOf("Gruber", "Piselli"),89.3),
)
collection.insertMany(students)

// :snippet-start: aggregates-data-class
val pipeline = listOf(
count(Student::name)
)

val result = collection.aggregate(pipeline)
result.collect { println(it) }
// :snippet-end:
}
}
21 changes: 0 additions & 21 deletions examples/src/test/kotlin/DataClassTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import com.mongodb.client.model.Filters
import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.FindOneAndUpdateOptions
import com.mongodb.client.model.ReturnDocument
import com.mongodb.client.model.Updates
Expand Down Expand Up @@ -97,26 +96,6 @@ internal class DataClassTest {
// :snippet-end:
}

@Test
fun queryDataClassTest() = runBlocking {

val collection = database.getCollection<DataStorage>("data_storage")

// :snippet-start: filters-query-data-class
val record = DataStorage("SSD", 120.0)
// Infixed query
DataStorage::productName.eq(record.productName)
// Nested query
val bson = eq(DataStorage::productName, record.productName)
// Kmongo DSL
val filter = DataStorage::productName eq record.productName
// :snippet-end:

collection.insertOne(record)
val result = collection.find().firstOrNull()
assertEquals(record, result)
}

// :snippet-start: annotated-data-class
data class NetworkDevice(
@BsonId
Expand Down
6 changes: 3 additions & 3 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
driver = "kotlin"
driver-short = "Kotlin driver"
driver-long = "MongoDB Kotlin Driver"
version = "5.2"
full-version = "{+version+}.1"
version = "5.3"
full-version = "{+version+}.0"
language = "Kotlin"
mdb-server = "MongoDB server"
kotlin-docs = "https://kotlinlang.org"
Expand All @@ -34,5 +34,5 @@ snappyVersion = "org.xerial.snappy:snappy-java:1.1.8.4"
zstdVersion = "com.github.luben:zstd-jni:1.5.5-2"
logbackVersion = "1.2.11"
log4j2Version = "2.17.1"
serializationVersion = "1.5.1"
serializationVersion = "1.7.3"
kotlinx-dt-version = "0.6.1"
17 changes: 11 additions & 6 deletions source/api-documentation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ API Documentation
:maxdepth: 1

BSON kotlinx.serialization <{+api+}/apidocs/bson-kotlinx/index.html>
Core <{+api+}/apidocs/mongodb-driver-core/index.html>
Kotlin Coroutine Driver <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/index.html>
Kotlin Sync Driver <{+api+}/apidocs/mongodb-driver-kotlin-sync/index.html>
{+language+} Driver Extensions <{+api+}/apidocs/mongodb-driver-kotlin-extensions/index.html>
Driver Core <{+api+}/apidocs/mongodb-driver-core/index.html>
{+language+} Coroutine Driver <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/index.html>
{+language+} Sync Driver <{+api+}/apidocs/mongodb-driver-kotlin-sync/index.html>

- `BSON kotlinx.serialization <{+api+}/apidocs/bson-kotlinx/index.html>`__ -
classes for encoding and decoding between Kotlin data classes and the BSON data
format using :github:`kotlinx.serialization <Kotlin/kotlinx.serialization>`.
- `Core <{+api+}/apidocs/mongodb-driver-core/index.html>`__ - classes that
- `{+language+} Driver Extensions
<{+api+}/apidocs/mongodb-driver-kotlin-extensions/index.html>`__ -
classes that extend the core builder classes to support :ref:`data
classes <kotlin-builders-data-classes>`.
- `Driver Core <{+api+}/apidocs/mongodb-driver-core/index.html>`__ - classes that
contain essential driver functionality.
- `Kotlin Coroutine Driver <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/index.html>`__ -
- `{+language+} Coroutine Driver <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/index.html>`__ -
classes for the current driver API using coroutines.
- `Kotlin Sync Driver <{+api+}/apidocs/mongodb-driver-kotlin-sync/index.html>`__ -
- `{+language+} Sync Driver <{+api+}/apidocs/mongodb-driver-kotlin-sync/index.html>`__ -
classes for the current synchronous driver API.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Data class to store aggregation result
data class Summary ( val average: Double )

val pipeline = listOf(
// Sorts grades from high to low
sort(Sorts.descending(Student::gradeAverage)),
// Selects the top 3 students
limit(3),
// Calculates the average of their grades and stores value in a Summary instance
group(null, avg(Summary::average, "\$${Student::gradeAverage.name}"))
)

val result = collection.aggregate<Summary>(pipeline)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data class Student(
val name: String,
val teachers: List<String>,
val gradeAverage: Double
)
Loading

0 comments on commit 508d1ca

Please sign in to comment.