Create a MongoClient
Overview
To connect to a MongoDB deployment, you need two things:
A connection URI, also known as a connection string, which tells the Scala driver which MongoDB deployment to connect to.
A MongoClient object, which creates the connection to and allows you to perform operations on the MongoDB deployment.
You can also use MongoClientSettings
to customize the way the Scala driver behaves
while connected to MongoDB.
This guide shows you how to create a connection string and use a MongoClient
object
to connect to MongoDB.
Connection URI
A standard connection string includes the following components:
Component | Description |
---|---|
| Required. A prefix that identifies this as a string in the standard connection format. |
| Optional. Authentication credentials. If you include these, the client
authenticates the user against the database specified in |
| Required. The host and optional port number where MongoDB is running. If you don't
include the port number, the driver uses the default port, |
| Optional. The authentication database to use if the
connection string includes |
| Optional. A query string that specifies connection-specific
options as |
For more information about creating a connection string, see Connection Strings in the MongoDB Server manual.
Atlas Connection Example
To connect to a MongoDB deployment on Atlas, you must first create a client.
You can pass a connection URI as a string to the MongoClient.create()
method
to connect to a MongoDB instance:
// Replace the placeholder with your Atlas connection string val connectionString = "<connection string>" // Create a new client and connect to the server val mongoClient = MongoClient(connectionString) val database = mongoClient.getDatabase("sample_mflix")
You can also create a client with your desired configurations by passing a
MongoClientSettings
object to a MongoClient
object.
To instantiate a MongoClientSettings
object, call the builder()
method,
then chain the applyConnectionString()
method and pass in your connection
string. You can also use the builder()
method to specify any other client
options. Once you have your desired configuration, call the build()
method.
You can set the Stable API version client option to avoid breaking changes when you upgrade to a new server version. To learn more about the Stable API feature, see the Stable API page.
The following code shows how you can specify the connection string and the Stable API client option when connecting to a MongoDB deployment on Atlas and verify that the connection is successful:
import com.mongodb.{ServerApi, ServerApiVersion} import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings} 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 placeholder with your Atlas connection string val connectionString = "<connection string>" // Construct a ServerApi instance using the ServerApi.builder() method val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build() val settings = MongoClientSettings .builder() .applyConnectionString(ConnectionString(connectionString)) .serverApi(serverApi) .build() // Create a new client and connect to the server Using(MongoClient(settings)) { mongoClient => // Send a ping to confirm a successful connection val database = mongoClient.getDatabase("admin") val ping = database.runCommand(Document("ping" -> 1)).head() Await.result(ping, 10.seconds) System.out.println("Pinged your deployment. You successfully connected to MongoDB!") } } }
API Documentation
For more information about creating a MongoClient
object with the
Scala driver, see the following API documentation: