Created
March 17, 2023 14:22
-
-
Save rozza/56ced5ff0198b3a32629bfd61a70bd56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mongodb.scala.MongoClient | |
import org.mongodb.scala.bson.Document | |
import scala.concurrent.Await | |
import scala.concurrent.duration.DurationInt | |
import scala.util.Using | |
object MongoClientConnectionExample { | |
def main(args: Array[String]): Unit = { | |
// Replace the placeholders with your credentials and hostname | |
val connectionString = "mongodb+srv://<username>:<password>@<svrHostName>"; | |
Using(MongoClient(connectionString)) { mongoClient => | |
val database = mongoClient.getDatabase("admin") | |
val ping = database.runCommand(Document("ping" -> 1)).head() | |
Await.result(ping, 10.seconds) // Wait for future to return | |
System.out.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