From caaa90f04ab7ce5933f3ab947d73097cff135ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 22 Mar 2026 22:35:21 +0100 Subject: [PATCH 1/2] feat(driver-coroutines): Add MongoCollection.exists() --- .../kotlin/operations/CountOperations.kt | 30 +++++++++++++++++++ .../commonTest/kotlin/BasicReadWriteTest.kt | 2 ++ 2 files changed, 32 insertions(+) diff --git a/driver-coroutines/src/commonMain/kotlin/operations/CountOperations.kt b/driver-coroutines/src/commonMain/kotlin/operations/CountOperations.kt index d2af4dad..96acd4f1 100644 --- a/driver-coroutines/src/commonMain/kotlin/operations/CountOperations.kt +++ b/driver-coroutines/src/commonMain/kotlin/operations/CountOperations.kt @@ -61,6 +61,36 @@ interface CountOperations : BaseOperations { predicate: FilterQuery.() -> Unit, ): Long + /** + * Tests if there exists a document that matches [predicate] in the collection. + * + * This method is a convenience function for calling [count] with a [limit][CountOptions.limit] of 1. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val age: Int, + * ) + * + * collection.exists { + * User::name eq "foo" + * User::age eq 10 + * } + * ``` + */ + suspend fun exists( + options: CountOptions.() -> Unit = {}, + predicate: FilterQuery.() -> Unit, + ): Boolean = count( + options = { + options() + limit(1) + }, + predicate = predicate, + ) == 1L + /** * Counts all documents in the collection. * diff --git a/test/src/commonTest/kotlin/BasicReadWriteTest.kt b/test/src/commonTest/kotlin/BasicReadWriteTest.kt index cc2abc46..54ceaadb 100644 --- a/test/src/commonTest/kotlin/BasicReadWriteTest.kt +++ b/test/src/commonTest/kotlin/BasicReadWriteTest.kt @@ -43,6 +43,8 @@ val BasicReadWriteTest by preparedSuite(preparedConfig = CoroutineTimeout(30.sec users().insertOne(User(_id = id1, name = "Bob", age = 18)) check(User(_id = id1, "Bob", age = 18) in users().find().toList()) + + check(users().exists { User::_id eq id1 }) } test("Simple upsert and read") { -- 2.51.2 From 9f32867e9482c36065ebdaa5fd1c76110b00bf2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 22 Mar 2026 22:35:59 +0100 Subject: [PATCH 2/2] feat(driver-sync): Add MongoCollection.exists() --- .../kotlin/operations/CountOperations.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/driver-sync/src/commonMain/kotlin/operations/CountOperations.kt b/driver-sync/src/commonMain/kotlin/operations/CountOperations.kt index 708d06af..5e663f96 100644 --- a/driver-sync/src/commonMain/kotlin/operations/CountOperations.kt +++ b/driver-sync/src/commonMain/kotlin/operations/CountOperations.kt @@ -61,6 +61,36 @@ interface CountOperations : BaseOperations { predicate: FilterQuery.() -> Unit, ): Long + /** + * Tests if there exists a document that matches [predicate] in the collection. + * + * This method is a convenience function for calling [count] with a [limit][CountOptions.limit] of 1. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val age: Int, + * ) + * + * collection.exists { + * User::name eq "foo" + * User::age eq 10 + * } + * ``` + */ + fun exists( + options: CountOptions.() -> Unit = {}, + predicate: FilterQuery.() -> Unit, + ): Boolean = count( + options = { + options() + limit(1) + }, + predicate = predicate, + ) == 1L + /** * Counts all documents in the collection. * -- 2.51.2