Skip to content

Commit a29f0ab

Browse files
authored
DOCSP-30246: kotlin aggregation expression operations (#86)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - [DOCSP-30246](https://jira.mongodb.org/browse/DOCSP-30246) [**Staging**](https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-30246-kotlin-agg-exp/fundamentals/aggregation-expression-operations/) ## Self-Review Checklist - [x] Is this free of any warnings or errors in the RST? - [x] Did you run a spell-check? - [ ] Did you run a grammar-check? - [x] Are all the links working?
1 parent f2c2e68 commit a29f0ab

18 files changed

+1777
-1
lines changed

examples/src/test/kotlin/AggExpressionsTest.kt

Lines changed: 529 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val month = current().getDate("date").month(of("UTC"))
2+
val precip = current().getInteger("precipitation")
3+
4+
listOf(
5+
Aggregates.group(
6+
month,
7+
Accumulators.avg("avgPrecipMM", precip.multiply(25.4))
8+
))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
val showtimes = current().getArray<MqlDocument>("showtimes")
2+
3+
listOf(
4+
Aggregates.project(
5+
Projections.fields(
6+
Projections.computed("availableShowtimes", showtimes
7+
.filter { showtime ->
8+
val seats = showtime.getArray<MqlInteger>("seats")
9+
val totalSeats = seats.sum { n -> n }
10+
val ticketsBought = showtime.getInteger("ticketsBought")
11+
val isAvailable = ticketsBought.lt(totalSeats)
12+
isAvailable
13+
})
14+
)))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val temperature = current().getInteger("temperature")
2+
3+
listOf(
4+
Aggregates.project(
5+
Projections.fields(
6+
Projections.computed("extremeTemp", temperature
7+
.lt(of(10))
8+
.or(temperature.gt(of(95))))
9+
)))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val location = current().getString("location")
2+
3+
listOf(
4+
Aggregates.match(
5+
Filters.expr(location.eq(of("California")))
6+
))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
val member = current().getField("member")
2+
3+
listOf(
4+
Aggregates.project(
5+
Projections.fields(
6+
Projections.computed("membershipLevel",
7+
member.switchOn{field -> field
8+
.isString{s-> s}
9+
.isBoolean{b -> b.cond(of("Gold"), of("Guest"))}
10+
.isArray { a -> a.last()}
11+
.defaults{ d -> of("Guest")}})
12+
)))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fun gradeAverage(students: MqlArray<MqlDocument>, fieldName: String): MqlNumber {
2+
val sum = students.sum{ student -> student.getInteger(fieldName) }
3+
val avg = sum.divide(students.size())
4+
return avg
5+
}
6+
7+
fun evaluate(grade: MqlNumber, cutoff1: MqlNumber, cutoff2: MqlNumber): MqlString {
8+
val message = grade.switchOn{ on -> on
9+
.lte(cutoff1) { g -> of("Needs improvement") }
10+
.lte(cutoff2) { g -> of("Meets expectations") }
11+
.defaults{g -> of("Exceeds expectations")}}
12+
return message
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val students = current().getArray<MqlDocument>("students")
2+
3+
listOf(
4+
Aggregates.project(
5+
Projections.fields(
6+
Projections.computed("evaluation", students
7+
.passArrayTo { s -> gradeAverage(s, "finalGrade") }
8+
.passNumberTo { grade -> evaluate(grade, of(70), of(85)) })
9+
)))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val graduationYear = current().getString("graduationYear")
2+
3+
listOf(
4+
Aggregates.addFields(
5+
Field("reunionYear",
6+
graduationYear
7+
.parseInteger()
8+
.add(5))
9+
))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val deliveryDate = current().getString("deliveryDate")
2+
3+
listOf(
4+
Aggregates.match(
5+
Filters.expr(deliveryDate
6+
.parseDate()
7+
.dayOfWeek(of("America/New_York"))
8+
.eq(of(2))
9+
)))

0 commit comments

Comments
 (0)