diff --git a/driver-api/src/commonMain/kotlin/MongoCollection.kt b/driver-api/src/commonMain/kotlin/MongoCollection.kt index c3e3d568..3f89b3e6 100644 --- a/driver-api/src/commonMain/kotlin/MongoCollection.kt +++ b/driver-api/src/commonMain/kotlin/MongoCollection.kt @@ -52,6 +52,7 @@ import kotlin.reflect.KType * - [Size limits](https://www.mongodb.com/docs/manual/reference/limits/#bson-documents) */ interface MongoCollection : ObjectIdGenerator, + CollectionOperations, CountOperations, DeleteOperations, FindOperations, diff --git a/driver-api/src/commonMain/kotlin/operations/CollectionOperations.kt b/driver-api/src/commonMain/kotlin/operations/CollectionOperations.kt new file mode 100644 index 00000000..00d6dc45 --- /dev/null +++ b/driver-api/src/commonMain/kotlin/operations/CollectionOperations.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2026, OpenSavvy and contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opensavvy.ktmongo.api.operations + +import opensavvy.ktmongo.dsl.command.DropOptions + +/** + * Interface grouping MongoDB operations relating to collection administration. + */ +interface CollectionOperations : BaseOperations { + + /** + * Removes an entire collection from the database. + * + * All documents within the collection are deleted. + * + * Indexes attached to this collection are also deleted. + * + * ### Example + * + * ```kotlin + * collection.drop() + * ``` + * + * ### External resources + * + * - [Protocol documentation](https://www.mongodb.com/docs/manual/reference/command/drop/) + * - [`mongosh` documentation](https://www.mongodb.com/docs/manual/reference/method/db.collection.drop/) + */ + suspend fun drop( + options: DropOptions.() -> Unit = {}, + ) + +} diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoCollectionImpl.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoCollectionImpl.kt index 47a269c5..13c41752 100644 --- a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoCollectionImpl.kt +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoCollectionImpl.kt @@ -191,6 +191,18 @@ private class CoroutineMongoCollectionImpl( ) } + // endregion + // region Collection + + @OptIn(LowLevelApi::class) + override suspend fun drop(options: DropOptions.() -> Unit) { + val model = Drop(context) + + model.options.options() + + inner.withWriteConcern(model.options).drop() + } + // endregion // region Update diff --git a/test/src/commonMain/kotlin/Entrypoint.kt b/test/src/commonMain/kotlin/Entrypoint.kt index 26ab8041..93ed8a78 100644 --- a/test/src/commonMain/kotlin/Entrypoint.kt +++ b/test/src/commonMain/kotlin/Entrypoint.kt @@ -78,4 +78,5 @@ fun SuiteDsl.verifyClient( verifyFindOperations(client) verifyUpdateOperations(client) verifyUpdatePipelineOperations(client) + verifyCollectionOperations(client) } diff --git a/test/src/commonMain/kotlin/operations/CollectionOperations.test.kt b/test/src/commonMain/kotlin/operations/CollectionOperations.test.kt new file mode 100644 index 00000000..47093f70 --- /dev/null +++ b/test/src/commonMain/kotlin/operations/CollectionOperations.test.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2026, OpenSavvy and contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opensavvy.ktmongo.tests.api.operations + +import kotlinx.serialization.Serializable +import opensavvy.ktmongo.api.MongoClient +import opensavvy.ktmongo.bson.types.ObjectId +import opensavvy.ktmongo.tests.api.collection +import opensavvy.prepared.suite.Prepared +import opensavvy.prepared.suite.SuiteDsl + +@Serializable +data class CollectionOperationsUser( + val _id: ObjectId, + val name: String, +) + +fun SuiteDsl.verifyCollectionOperations( + client: Prepared, +) = suite("Collection operations") { + val collection by client.collection("operation-collection-users") + + test("Drop an empty collection") { + collection().drop() + } + + test("Drop a non-empty collection") { + collection().insertMany( + CollectionOperationsUser( + _id = collection().newId(), + name = "Alice", + ), + CollectionOperationsUser( + _id = collection().newId(), + name = "Bob", + ), + ) + + collection().drop() + + check(collection().count() == 0L) + } +}