[BUG] Incorrect dropCollection()
VincentTam opened this issue · comments
Thanks for great tutorial. I'm new to both Kotlin and MongoDB (Atlas).
kotlin-driver-quick-start/src/main/kotlin/Setup.kt
Lines 61 to 63 in c236e4f
On Mohit's tutorial, it says
https://www.mongodb.com/developer/products/mongodb/getting-started-kotlin-driver/
suspend fun dropCollection(database: MongoDatabase) {
database.getCollection<Objects>(collectionName = "restaurants").drop()
}
The former gave NamespaceNotFound
error, cuz the "collection" collectionName
didn't exist.
10:58:12.611 [AsyncGetter-7-thread-1] DEBUG org.mongodb.driver.protocol.command - Command "drop" started on database sample_restaurants using a connection with driver-generated ID 7 and server-generated ID 9902 to ac-hvvz70k-shard-00-02.vgkddrs.mongodb.net:27017. The request ID is 16 and the operation ID is 13. Command: {"drop": "collectionName", "writeConcern": {"w": "majority"}, "$db": "sample_restaurants", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1693472292, "i": 5}}, "signature": {"hash": {"$binary": {"base64": "Md/e/RXCwkBoZYHkPCV8mtxVsXI=", "subType": "00"}}, "keyId": 7230892126479843330}}, "lsid": {"id": {"$binary": {"base64": "RU6FhcJ5TJmbsxeZpSxHtA==", "subType": "04"}}}}
10:58:12.642 [async-channel-group-0-handler-executor] DEBUG org.mongodb.driver.protocol.command - Command "drop" failed in 18.6061 ms using a connection with driver-generated ID 7 and server-generated ID 9902 to ac-hvvz70k-shard-00-02.vgkddrs.mongodb.net:27017. The request ID is 16 and the operation ID is 13.
com.mongodb.MongoCommandException: Command failed with error 26 (NamespaceNotFound): 'ns not found' on server ac-hvvz70k-shard-00-02.vgkddrs.mongodb.net:27017. The full response is {"ok": 0.0, "errmsg": "ns not found", "code": 26, "codeName": "NamespaceNotFound", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1693472292, "i": 5}}, "signature": {"hash": {"$binary": {"base64": "Md/e/RXCwkBoZYHkPCV8mtxVsXI=", "subType": "00"}}, "keyId": 7230892126479843330}}, "operationTime": {"$timestamp": {"t": 1693472292, "i": 5}}}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205)
at com.mongodb.internal.connection.InternalStreamConnection.lambda$sendCommandMessageAsync$0(InternalStreamConnection.java:544)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:847)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:810)
at com.mongodb.internal.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:669)
at com.mongodb.internal.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:666)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:251)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:234)
at com.mongodb.internal.connection.tlschannel.async.AsynchronousTlsChannel.lambda$read$4(AsynchronousTlsChannel.java:122)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
@VincentTam: Thanks for reaching out.
I have hardcoded the collection name with collectionName
on purpose to avoid accidental dropping of the collection while running the code. Feel free to replace it with your collection name to test the functionality.