How Can I Close MongoDB Connection In KMongo?

I have searched most of the docs but I couldn’t find anything, do you know how I can close the MongoDB connection in KMongo?

Thank you:)

Here is a previous discussion on closing a MongoDB connection with Kotlin does this help?

1 Like

so, I got the solution; if anyone is looking for the same then this may help you:-
by using .also{} method the problem was solved;

get("/somePath") {
  val client= KMongo.createClient("mongodb+srv://${System.getenv("USER_NAME")}:${System.getenv("PASSWORD")}")
  val clientResponse = client.getDatabase(System.getenv("DATABASE_NAME")).getCollection(System.getenv("COLLECTION_NAME"))
  call.respond(clientResponse).also {
      client.close() // closes the connection after responding

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