From 6594952618bba3cb147782d5bb9e6d0ef8ec4a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 2 Aug 2026 10:44:31 +0200 Subject: [PATCH] test: Add an integration test for the $lookup case with an uncorrelated subquery --- .../kotlin/studies/AggregationLookup.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test-legacy/src/commonTest/kotlin/studies/AggregationLookup.kt b/test-legacy/src/commonTest/kotlin/studies/AggregationLookup.kt index 33c6eb13..3b8fa30a 100644 --- a/test-legacy/src/commonTest/kotlin/studies/AggregationLookup.kt +++ b/test-legacy/src/commonTest/kotlin/studies/AggregationLookup.kt @@ -52,6 +52,14 @@ data class Comment( val year: Int = 0, ) +@Serializable +data class User( + val _id: ObjectId, + val name: String, + val email: String, + val long_movies: List = emptyList(), +) + @Serializable data class SchoolClass( val _id: ObjectId, @@ -74,6 +82,7 @@ val AggregationLookup by preparedSuite { val comments by testCollection("case-lookup-comments") val classes by testCollection("case-lookup-classes") val students by testCollection("case-lookup-students") + val users by testCollection("case-lookup-users") test($$"Perform a Single Equality Join with $lookup") { // Ported from: https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#perform-a-single-equality-join-with--lookup @@ -282,4 +291,57 @@ val AggregationLookup by preparedSuite { check(result.first { it._id == kafka }.post_release_comments.map { it.name } == listOf("Khal Drogo")) check(result.first { it._id == corpseBride }.post_release_comments.isEmpty()) } + + test($$"Perform an Uncorrelated Subquery with $lookup") { + // Ported from: https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#perform-an-uncorrelated-subquery-with--lookup + + val robert = users().newId() + val cersei = users().newId() + val other = users().newId() + + users().insertMany( + User(_id = robert, name = "Robert Baratheon", email = "mark_addy@gameofthron.es"), + User(_id = cersei, name = "Cersei Lannister", email = "lena_headey@gameofthron.es"), + User(_id = other, name = "Someone Else", email = "someone@example.com"), + ) + + val centennial = movies().newId() + val baseball = movies().newId() + val shortMovie = movies().newId() + + movies().insertMany( + Movie(_id = centennial, runtime = 1200, title = "Centennial", year = 1978), + Movie(_id = baseball, runtime = 1140, title = "Baseball", year = 1994), + Movie(_id = shortMovie, runtime = 100, title = "Short Movie", year = 2000), + ) + + val allMovies = movies().aggregate() + + val result = users().aggregate() + // { $match: { email: { $in: [ "mark_addy@gameofthron.es", "lena_headey@gameofthron.es" ] } } }, + .match { User::email isOneOf listOf("mark_addy@gameofthron.es", "lena_headey@gameofthron.es") } + // { + // $lookup: { + // from: "movies", + // pipeline: [ + // { $match: { runtime: { $gt: 1000 } } }, + // { $project: { _id: 0, title: 1, year: 1 } } + // ], + // as: "long_movies" + // } + // }, + .lookup { + into(User::long_movies) + from( + allMovies + .match { Movie::runtime gt 1000 } + ) + } + .toList() + + check(result.none { it._id == other }) + + check(result.first { it._id == robert }.long_movies.map { it.title }.sorted() == listOf("Baseball", "Centennial")) + check(result.first { it._id == cersei }.long_movies.map { it.title }.sorted() == listOf("Baseball", "Centennial")) + } } -- 2.51.2