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