|
| 1 | +.. _java-rs-connection-targets: |
| 2 | + |
| 3 | +========================== |
| 4 | +Choose a Connection Target |
| 5 | +========================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: connection string, URI, server, settings, client |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use a connection string and a ``MongoClient`` object |
| 24 | +to connect to different types of MongoDB deployments. |
| 25 | + |
| 26 | +Atlas |
| 27 | +----- |
| 28 | + |
| 29 | +To connect to a MongoDB deployment on Atlas, include the following elements |
| 30 | +in your connection string: |
| 31 | + |
| 32 | +- URL of your Atlas cluster |
| 33 | +- MongoDB username |
| 34 | +- MongoDB password |
| 35 | + |
| 36 | +Then, pass your connection string to the ``create()`` method constructing a ``MongoClient`` object. |
| 37 | + |
| 38 | +.. tip:: |
| 39 | + |
| 40 | + Follow the :atlas:`Atlas driver connection guide </driver-connection>` |
| 41 | + to retrieve your connection string. |
| 42 | + |
| 43 | +When you connect to Atlas, we recommend using the Stable API client option to avoid |
| 44 | +breaking changes when Atlas upgrades to a new version of MongoDB Server. |
| 45 | +To learn more about the Stable API feature, see :manual:`Stable API |
| 46 | +</reference/stable-api>` in the MongoDB Server manual. |
| 47 | + |
| 48 | +The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The |
| 49 | +code uses the ``serverApi`` option to specify a Stable API version. |
| 50 | + |
| 51 | +.. include:: /includes/connect/connect-reactor.rst |
| 52 | + |
| 53 | +.. literalinclude:: /includes/connect/atlas-connection.java |
| 54 | + :language: java |
| 55 | + |
| 56 | +Local Deployments |
| 57 | +----------------- |
| 58 | + |
| 59 | +To connect to a local MongoDB deployment, use ``localhost`` as the hostname. By |
| 60 | +default, the ``mongod`` process runs on port 27017, though you can customize this for |
| 61 | +your deployment. |
| 62 | + |
| 63 | +The following code shows how to use the {+driver-short+} to connect to a local MongoDB |
| 64 | +deployment: |
| 65 | + |
| 66 | +.. literalinclude:: /includes/connect/choose-connect-target.java |
| 67 | + :start-after: start-connect-local |
| 68 | + :end-before: end-connect-local |
| 69 | + :language: java |
| 70 | + :copyable: |
| 71 | + |
| 72 | +Replica Sets |
| 73 | +------------ |
| 74 | + |
| 75 | +To connect to a replica set, specify the hostnames (or IP addresses) and |
| 76 | +port numbers of the replica-set members in your connection string. |
| 77 | + |
| 78 | +If you aren't able to provide a full list of hosts in the replica set, you can |
| 79 | +specify one or more of the hosts in the replica set and instruct the {+driver-short+} to |
| 80 | +perform automatic discovery to find the others. To instruct the driver to perform |
| 81 | +automatic discovery, perform one of the following actions: |
| 82 | + |
| 83 | +- Specify the name of the replica set as the value of the ``replicaSet`` parameter. |
| 84 | +- Specify ``false`` as the value of the ``directConnection`` parameter. |
| 85 | +- Specify more than one host in the replica set. |
| 86 | + |
| 87 | +In the following example, the driver uses a sample connection URI to connect to the |
| 88 | +MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different |
| 89 | +hosts, including ``host1``: |
| 90 | + |
| 91 | +.. literalinclude:: /includes/connect/choose-connect-target.java |
| 92 | + :start-after: start-connect-replica |
| 93 | + :end-before: end-connect-replica |
| 94 | + :language: java |
| 95 | + :copyable: |
| 96 | + |
| 97 | +.. note:: create() is Non-Blocking |
| 98 | + |
| 99 | + The ``create()`` method constructing a ``MongoClient`` is *non-blocking*. |
| 100 | + When you connect to a replica set, the method returns immediately while the |
| 101 | + client uses background threads to connect to the replica set. |
| 102 | + |
| 103 | + If you create a ``MongoClient`` and immediately print the string representation |
| 104 | + of its ``nodes`` attribute, the list might be empty while the client connects to |
| 105 | + the replica-set members. |
| 106 | + |
| 107 | +Initialization |
| 108 | +~~~~~~~~~~~~~~ |
| 109 | + |
| 110 | +To initialize a replica set, you must connect directly to a single member. To do so, |
| 111 | +set the ``directConnection`` connection |
| 112 | +option to ``True``. You can do this in the following ways: |
| 113 | + |
| 114 | +- Passing an argument to the ``create()`` method constructing a ``MongoClient`` |
| 115 | +- Setting the parameter in your connection string. |
| 116 | + |
| 117 | +.. tabs:: |
| 118 | + |
| 119 | + .. tab:: MongoClient |
| 120 | + :tabid: mongoclient |
| 121 | + |
| 122 | + .. literalinclude:: /includes/connect/choose-connect-target.java |
| 123 | + :start-after: start-arg-constructor |
| 124 | + :end-before: end-arg-constructor |
| 125 | + :language: java |
| 126 | + :copyable: |
| 127 | + :emphasize-lines: 7 |
| 128 | + |
| 129 | + .. tab:: Connection String |
| 130 | + :tabid: connectionstring |
| 131 | + |
| 132 | + .. literalinclude:: /includes/connect/choose-connect-target.java |
| 133 | + :start-after: start-parameter-string |
| 134 | + :end-before: end-parameter-string |
| 135 | + :language: java |
| 136 | + :copyable: |
| 137 | + :emphasize-lines: 6 |
| 138 | + |
| 139 | +API Documentation |
| 140 | +----------------- |
| 141 | + |
| 142 | +To learn more about creating a ``MongoClient`` instance in the {+driver-short+}, |
| 143 | +see the following API documentation: |
| 144 | + |
| 145 | +- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__ |
0 commit comments