[SOLVED] Can't drop collections in Kotlin

I’m following this tutorial on Kotlin Multi-platform (KMM) with MongoDB Atlas.

which points to

I managed to listAllCollection(database)


but the function dropCollection(database) didn’t work.

suspend fun dropCollection(database: MongoDatabase) {
    database.getCollection<Objects>(collectionName = "collectionName").drop()
}

I’m getting

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)

Environment

  • OS: MS Win10
  • JAVA version: 11.0.19
  • IDE: IntelliJ IDEA 2023.1 (Community Edition) Build #IC-231.8109.175, built on March 28, 2023

i solved the problem myself. it’s due to nonexistent collection.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.