-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-36218: builders with dataclass properties #186
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
rustagir
merged 21 commits into
mongodb:master
from
rustagir:DOCSP-36218-filters-data-class
Jan 24, 2025
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c86f88c
DOCSP-36218: filters with dataclass properties
rustagir e2575be
fixes
rustagir 6050c93
fixes
rustagir 508d1ca
PR restructure and pushing v5.3 changes
rustagir 20c31c6
cross links
rustagir b056571
fixes
rustagir f9fa810
fixes
rustagir 5239a20
shorten toc name
rustagir c1e534b
fix link
rustagir d06be1f
small version fixes
rustagir 0c0f1de
add cross link
rustagir c8a2c9c
RM PR fixes 1
rustagir 9311ba6
add note to Kmongo page
rustagir 050ac55
vale fixes
rustagir 7825e03
Merge branch 'master' into DOCSP-36218-filters-data-class
rustagir 2b5b815
merge upstream
rustagir 04ea69b
merge upstream
rustagir 82b8d67
NH tech review 2
rustagir 6fef9d5
wip
rustagir 19278b9
wip
rustagir 11d4686
wip
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
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) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
source/examples/generated/BuildersDataClassTest.snippet.aggregates-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
5 changes: 5 additions & 0 deletions
5
source/examples/generated/BuildersDataClassTest.snippet.data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
15 changes: 15 additions & 0 deletions
15
source/examples/generated/BuildersDataClassTest.snippet.filters-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 |
5 changes: 5 additions & 0 deletions
5
source/examples/generated/BuildersDataClassTest.snippet.indexes-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
val ascendingIdx = Indexes.ascending(Student::name) | ||
val descendingIdx = Indexes.descending(Student::teachers) | ||
|
||
val ascIdxName = collection.createIndex(ascendingIdx) | ||
val descIdxName = collection.createIndex(descendingIdx) |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/BuildersDataClassTest.snippet.projections-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
val combinedProj = fields( | ||
include(Student::name, Student::gradeAverage), | ||
excludeId() | ||
) | ||
|
||
collection.find().projection(combinedProj) |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/BuildersDataClassTest.snippet.sorts-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
val sort = orderBy( | ||
Sorts.descending(Student::gradeAverage), | ||
Sorts.ascending(Student::name) | ||
) | ||
|
||
collection.find().sort(sort) |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/BuildersDataClassTest.snippet.updates-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
val filter = Student::gradeAverage gte 85.0 | ||
val update = combine( | ||
addToSet(Student::teachers, "Soto"), | ||
Student::gradeAverage.max(90.0) | ||
) | ||
collection.updateMany(filter, update) |
7 changes: 7 additions & 0 deletions
7
source/examples/generated/DataClassTest.snippet.filters-query-data-class.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this referenced somewhere?