|
| 1 | +.. _kotlin-sync-connect: |
| 2 | + |
| 3 | +================== |
| 4 | +Connect to MongoDB |
| 5 | +================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :description: Learn how to use the Kotlin Sync driver to connect to MongoDB. |
| 19 | + :keywords: client, ssl, tls, localhost |
| 20 | + |
| 21 | +.. toctree:: |
| 22 | + :titlesonly: |
| 23 | + :maxdepth: 1 |
| 24 | + |
| 25 | + /connect/stable-api |
| 26 | + /connect/connection-targets |
| 27 | + |
| 28 | +.. /connect/mongoclient |
| 29 | +.. /connect/connection-options |
| 30 | +.. /connect/tls |
| 31 | +.. /connect/network-compression |
| 32 | +.. /connect/server-selection |
| 33 | +.. /connect/csot |
| 34 | + |
| 35 | +Overview |
| 36 | +-------- |
| 37 | + |
| 38 | +This page contains code examples that show how to use the |
| 39 | +{+driver-short+} to connect your application to MongoDB by specifying |
| 40 | +various settings. |
| 41 | + |
| 42 | +.. .. tip:: |
| 43 | +.. |
| 44 | +.. To learn more about the connection options on this page, see the link |
| 45 | +.. provided in each section. |
| 46 | + |
| 47 | +To use a connection example from this page, copy the code example into the |
| 48 | +:ref:`sample application <kotlin-sync-connect-sample>` or your own application. |
| 49 | +Be sure to replace all placeholders in the code examples, such as |
| 50 | +``<hostname>``, with the relevant values for your MongoDB deployment. |
| 51 | + |
| 52 | +.. _kotlin-sync-connect-sample: |
| 53 | + |
| 54 | +.. include:: /includes/usage-examples/sample-app-intro.rst |
| 55 | + |
| 56 | +.. literalinclude:: /includes/usage-examples/connect-sample-app.kt |
| 57 | + :language: kotlin |
| 58 | + :copyable: true |
| 59 | + :linenos: |
| 60 | + :emphasize-lines: 6-8 |
| 61 | + |
| 62 | +Connection |
| 63 | +---------- |
| 64 | + |
| 65 | +The following sections describe how to connect to different targets, |
| 66 | +such as a local instance of MongoDB or a cloud-hosted instance on Atlas. |
| 67 | + |
| 68 | +Local Deployment |
| 69 | +~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +The following code shows the connection string to connect to a local |
| 72 | +instance of MongoDB: |
| 73 | + |
| 74 | +.. code-block:: kotlin |
| 75 | + |
| 76 | + val uri = "mongodb://localhost:27017/" |
| 77 | + val mongoClient = MongoClient.create(uri) |
| 78 | + |
| 79 | +Atlas |
| 80 | +~~~~~ |
| 81 | + |
| 82 | +The following code shows the connection string to connect to a |
| 83 | +deployment hosted on Atlas: |
| 84 | + |
| 85 | +.. code-block:: kotlin |
| 86 | + |
| 87 | + val uri = "mongodb+srv://<username>:<password>@<hostname/port>/?<options>" |
| 88 | + val mongoClient = MongoClient.create(uri) |
| 89 | + |
| 90 | +Replica Set |
| 91 | +~~~~~~~~~~~ |
| 92 | + |
| 93 | +The following code shows the connection string to connect to a |
| 94 | +replica set: |
| 95 | + |
| 96 | +.. code-block:: kotlin |
| 97 | + |
| 98 | + val uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>" |
| 99 | + val mongoClient = MongoClient.create(uri) |
| 100 | + |
| 101 | +Transport Layer Security (TLS) |
| 102 | +------------------------------ |
| 103 | + |
| 104 | +The following sections describe how to connect to MongoDB |
| 105 | +while enabling the TLS protocol. |
| 106 | + |
| 107 | +Enable TLS |
| 108 | +~~~~~~~~~~ |
| 109 | + |
| 110 | +The following tabs demonstrate how to enable TLS on a connection: |
| 111 | + |
| 112 | +.. include:: /includes/connect/tls-tabs.rst |
| 113 | + |
| 114 | +.. To learn more about enabling TLS, see :ref:`kotlin-sync-enable-tls` in |
| 115 | +.. the TLS configuration guide. |
| 116 | + |
| 117 | +Disable Hostname Verification |
| 118 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 | + |
| 120 | +The following tabs demonstrate how to disable hostname verification when |
| 121 | +connecting by using TLS: |
| 122 | + |
| 123 | +.. include:: /includes/connect/disable-host-verification-tabs.rst |
| 124 | + |
| 125 | +.. To learn more about disabling hostname verification, see :ref:`kotlin-sync-insecure-tls` in |
| 126 | +.. the TLS configuration guide. |
| 127 | + |
| 128 | +Network Compression |
| 129 | +------------------- |
| 130 | + |
| 131 | +The following sections describe how to connect to MongoDB |
| 132 | +while specifying network compression algorithms. |
| 133 | + |
| 134 | +Compression Algorithms |
| 135 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 136 | + |
| 137 | +The following tabs demonstrate how to specify all available compressors |
| 138 | +while connecting to MongoDB: |
| 139 | + |
| 140 | +.. include:: /includes/connect/compression-tabs.rst |
| 141 | + |
| 142 | +.. To learn more about specifying compression algorithms, see |
| 143 | +.. :ref:`kotlin-sync-enable-compression` in the Network Compression guide. |
| 144 | + |
| 145 | +zlib Compression Level |
| 146 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 147 | + |
| 148 | +The following tabs demonstrate how to specify a compression level for |
| 149 | +the ``zlib`` compressor: |
| 150 | + |
| 151 | +.. include:: /includes/connect/zlib-level-tabs.rst |
| 152 | + |
| 153 | +.. To learn more about setting the zlib compression level, see |
| 154 | +.. :ref:`kotlin-sync-enable-compression` in the Network Compression guide. |
| 155 | + |
| 156 | +Server Selection |
| 157 | +---------------- |
| 158 | + |
| 159 | +The following code shows a connection string that specifies a server |
| 160 | +selection function: |
| 161 | + |
| 162 | +.. code-block:: kotlin |
| 163 | + |
| 164 | + val client = MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>", |
| 165 | + server_selector=<selector function>) |
| 166 | + |
| 167 | +.. To learn more about customizing server selection, see |
| 168 | +.. :ref:`kotlin-sync-server-selection`. |
| 169 | + |
| 170 | +{+stable-api+} |
| 171 | +-------------- |
| 172 | + |
| 173 | +The following code shows how to specify Stable API settings within a |
| 174 | +``MongoClientSettings`` instance: |
| 175 | + |
| 176 | +.. code-block:: kotlin |
| 177 | + |
| 178 | + val serverApi = ServerApi.builder() |
| 179 | + .version(ServerApiVersion.V1) |
| 180 | + .build() |
| 181 | + |
| 182 | + val uri = "<connection string>" |
| 183 | + |
| 184 | + val settings = MongoClientSettings.builder() |
| 185 | + .applyConnectionString(ConnectionString(uri)) |
| 186 | + .serverApi(serverApi) |
| 187 | + .build() |
| 188 | + |
| 189 | + val client = MongoClient.create(settings) |
| 190 | + |
| 191 | +.. To learn more about the {+stable-api+}, see :ref:`kotlin-sync-stable-api`. |
0 commit comments