-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41122: Choose a Connection Target #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mcmorisi
merged 5 commits into
mongodb:master
from
mcmorisi:DOCSP-41122-connection-targets
Aug 13, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
.. _kotlin-sync-connection-targets: | ||
|
||
========================== | ||
Choose a Connection Target | ||
========================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, settings, client | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use a connection string and a ``MongoClient`` object | ||
to connect to different types of MongoDB deployments. | ||
|
||
Atlas | ||
----- | ||
|
||
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. | ||
|
||
.. tip:: | ||
|
||
Follow the :atlas:`Atlas driver connection guide </driver-connection?tck=docs_driver_python>` | ||
to retrieve your connection string. | ||
|
||
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 {+mdb-server+}. | ||
To learn more about the {+stable-api+} feature, see the :ref:`<kotlin-sync-stable-api>` | ||
guide. | ||
|
||
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The | ||
code also uses the ``serverApi()`` method to specify a {+stable-api+} version. | ||
|
||
.. literalinclude:: /includes/connect/connection-targets.kt | ||
:language: kotlin | ||
:start-after: start-connect | ||
:end-before: end-connect | ||
:dedent: | ||
|
||
Local Deployments | ||
----------------- | ||
|
||
To connect to a local MongoDB deployment, use ``localhost`` as the hostname. By | ||
default, the ``mongod`` process runs on port 27017, though you can customize this for | ||
your deployment. | ||
|
||
The following code shows how to use the {+driver-short+} to connect to a local MongoDB | ||
deployment: | ||
|
||
.. literalinclude:: /includes/connect/connection-targets.kt | ||
:language: kotlin | ||
:start-after: start-connect-local | ||
:end-before: end-connect-local | ||
:dedent: | ||
|
||
Replica Sets | ||
------------ | ||
|
||
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 {+driver-short+} 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 following examples show how to connect to a MongoDB replica set running on port | ||
``27017`` of three different hosts by using either the ``ConnectionString`` or | ||
``MongoClientSettings`` class. Select the tab that corresponds to your preferred class. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: ConnectionString | ||
:tabid: connectionstring | ||
|
||
.. literalinclude:: /includes/connect/connection-targets.kt | ||
:language: kotlin | ||
:start-after: start-connect-rs-connection-string | ||
:end-before: end-connect-rs-connection-string | ||
:dedent: | ||
|
||
.. tab:: MongoClientSettings | ||
:tabid: mongoclientsettings | ||
|
||
.. literalinclude:: /includes/connect/connection-targets.kt | ||
:language: kotlin | ||
:start-after: start-connect-rs-settings | ||
:end-before: end-connect-rs-settings | ||
:dedent: | ||
|
||
.. note:: | ||
|
||
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. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.example | ||
import com.mongodb.ConnectionString | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.ServerAddress | ||
import com.mongodb.ServerApi | ||
import com.mongodb.ServerApiVersion | ||
import com.mongodb.kotlin.client.MongoClient | ||
|
||
fun main() { | ||
// start-connect | ||
val connectionString = ConnectionString("<connection string URI>") | ||
|
||
// Defines Stable API version | ||
val serverApi = ServerApi.builder() | ||
.version(ServerApiVersion.V1) | ||
.build() | ||
|
||
// Uses MongoClientSettings to apply connection string and specify the Stable API version | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(connectionString) | ||
.serverApi(serverApi) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
// end-connect | ||
|
||
// start-connect-local | ||
val connectionString = ConnectionString("mongodb://localhost:27017") | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(connectionString) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
// end-connect-local | ||
|
||
// start-connect-rs-connection-string | ||
val connectionString = ConnectionString("mongodb://host1:27017,host2:27017,host3:27017/") | ||
val mongoClient = MongoClient.create(connectionString) | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// end-connect-rs-connection-string | ||
|
||
// start-connect-rs-settings | ||
val hosts = listOf( | ||
ServerAddress("host1", 27017), | ||
ServerAddress("host2", 27017), | ||
ServerAddress("host3", 27017) | ||
) | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyToClusterSettings { builder -> | ||
builder.hosts(hosts) | ||
} | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
// end-connect-rs-settings | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: tck param looks wrong (admittedly I dont know what it actually does 😁)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on what looks wrong? Unsure on what you're referring to.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies:
Atlas driver connection guide </driver-connection?tck=docs_driver_python>
Specifically:
docs_driver_python
. If its all good then LGTM :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, I can remove that. Good eye!