Skip to content

Instantly share code, notes, and snippets.

@rozza
Last active March 30, 2023 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rozza/d63bbbc8da7e73bc32ba0b43411e4572 to your computer and use it in GitHub Desktop.
Save rozza/d63bbbc8da7e73bc32ba0b43411e4572 to your computer and use it in GitHub Desktop.
Kotlin sync simple example of connecting to the database
import com.mongodb.kotlin.client.MongoClient
import org.bson.Document
object MongoClientConnectionExample {
fun main() {
// Replace the placeholders with your credentials and hostname
val connectionString = "mongodb+srv://<username>:<password>@<svrHostName>";
// Create a new client and connect to the server
MongoClient.create(connectionString).use { mongoClient ->
val database = mongoClient.getDatabase("admin")
database.runCommand(Document("ping", 1))
println("Pinged your deployment. You successfully connected to MongoDB!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment