In this guide, you can learn how to use a connection string and a MongoClient
object to connect to different types of MongoDB deployments.
To connect to a MongoDB deployment on Atlas, include the following elements in your connection string:
The URL of your Atlas cluster
Your MongoDB username
Your MongoDB password
Then, pass your connection string to the MongoClient
constructor.
When you connect to Atlas, we recommend using the Stable API client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. To learn more about the Stable API feature, see the Stable API guide.
The following code shows how to use the Scala driver to connect to an Atlas cluster. The code also uses the serverApi()
method to specify a Stable API version.
object MongoClientConnectToAtlas { def main(args: Array[String]): Unit = { val connectionString = "<connection string>"; val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build() val settings = MongoClientSettings .builder() .applyConnectionString(ConnectionString(connectionString)) .serverApi(serverApi) .build() Using(MongoClient(settings)) { mongoClient => 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!") } }}
You can connect to a local MongoDB deployment in the following ways:
Instantiate a MongoClient
object without any parameters to connect to a MongoDB server running on localhost
on port 27017
:
val mongoClient = MongoClient()
Explicitly specify the hostname
to connect to a MongoDB instance running on the specified host on port 27017
:
val mongoClient = MongoClient("mongodb://host1")
Explicitly specify the hostname
and the port
:
val mongoClient = MongoClient("mongodb://host1:27017")
To connect to a replica set, specify the hostnames (or IP addresses) and port numbers of the replica set members.
If you aren't able to provide a full list of hosts in the replica set, you can specify one or more of the hosts in the replica set and instruct the Scala driver to perform automatic discovery to find the others. To instruct the driver to perform automatic discovery, perform one of the following actions:
Specify the name of the replica set as the value of the replicaSet
parameter.
Specify false
as the value of the directConnection
parameter.
Specify more than one host in the replica set.
The MongoClient
constructor is non-blocking. When you connect to a replica set, the constructor returns immediately while the client uses background threads to connect to the replica set.
If you construct a MongoClient
and immediately print the string representation of its nodes
attribute, the list might be empty while the client connects to the replica-set members.
You can connect to a MongoDB replica set by specifying the members in a ConnectionString
. The following example shows how to specify three members of the replica set:
val mongoClient = MongoClient("mongodb://host1:27017,host2:27017,host3:27017")
The following example shows how to specify members of the replica set and the replicaSet
option with the replica set name:
val mongoClient = MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet")
The following example shows how to specify a list of ServerAddress
instances corresponding to all of the replica set members:
val mongoClient = MongoClient( MongoClientSettings.builder() .applyToClusterSettings((builder: ClusterSettings.Builder) => builder.hosts(List( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017), new ServerAddress("host3", 27017)).asJava)) .build())
Note Replica Set in Docker
When a replica set runs in Docker, it might expose only one MongoDB endpoint. In this case, the replica set is not discoverable. Specifying directConnection=false
in your connection URI, or leaving this option unset, can prevent your application from connecting to it.
In a test or development environment, you can connect to the replica set by specifying directConnection=true
. In a production environment, we recommend configuring the cluster to make each MongoDB instance accessible outside of the Docker virtual network.
To learn more about any of the methods or types discussed in this guide, see the following API documentation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4