|
| 1 | +.. _kotlin-sync-mongoclient: |
| 2 | + |
| 3 | +==================== |
| 4 | +Create a MongoClient |
| 5 | +==================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: connection string, URI, server, Atlas, 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 | +To connect to a MongoDB deployment, you need two things: |
| 24 | + |
| 25 | +- A **connection URI**, also known as a *connection string*, which tells the {+driver-short+} |
| 26 | + which MongoDB deployment to connect to. |
| 27 | +- A **MongoClient** object, which creates the connection to and performs |
| 28 | + operations on the MongoDB deployment. |
| 29 | + |
| 30 | +You can also use ``MongoClientSettings`` to customize the way the {+driver-short+} behaves |
| 31 | +while connected to MongoDB. |
| 32 | + |
| 33 | +This guide shows you how to create a connection string and use a ``MongoClient`` object |
| 34 | +to connect to MongoDB. |
| 35 | + |
| 36 | +.. _kotlin-sync-connection-uri: |
| 37 | + |
| 38 | +Connection URI |
| 39 | +-------------- |
| 40 | + |
| 41 | +A standard connection string includes the following components: |
| 42 | + |
| 43 | +.. TODO, add this as last sentence for ``username:password`` description once a kotlin auth page is made: |
| 44 | +.. For more information about the ``authSource`` connection option, see :ref:`kotlin-sync-auth`. |
| 45 | + |
| 46 | +.. list-table:: |
| 47 | + :widths: 20 80 |
| 48 | + :header-rows: 1 |
| 49 | + |
| 50 | + * - Component |
| 51 | + - Description |
| 52 | + |
| 53 | + * - ``mongodb://`` |
| 54 | + |
| 55 | + - Required. A prefix that identifies this as a string in the |
| 56 | + standard connection format. |
| 57 | + |
| 58 | + * - ``username:password`` |
| 59 | + |
| 60 | + - Optional. Authentication credentials. If you include these, the client |
| 61 | + authenticates the user against the database specified in ``authSource``. |
| 62 | + |
| 63 | + * - ``host[:port]`` |
| 64 | + |
| 65 | + - Required. The host and optional port number where MongoDB is running. If you don't |
| 66 | + include the port number, the driver uses the default port, ``27017``. |
| 67 | + |
| 68 | + * - ``/defaultauthdb`` |
| 69 | + |
| 70 | + - Optional. The authentication database to use if the |
| 71 | + connection string includes ``username:password@`` |
| 72 | + authentication credentials but not the ``authSource`` option. If you don't include |
| 73 | + this component, the client authenticates the user against the ``admin`` database. |
| 74 | + |
| 75 | + * - ``?<options>`` |
| 76 | + |
| 77 | + - Optional. A query string that specifies connection-specific |
| 78 | + options as ``<name>=<value>`` pairs. See |
| 79 | + :ref:`kotlin-sync-connection-options` for a full description of |
| 80 | + these options. |
| 81 | + |
| 82 | +For more information about creating a connection string, see |
| 83 | +:manual:`Connection Strings </reference/connection-string>` in the |
| 84 | +MongoDB Server documentation. |
| 85 | + |
| 86 | +Atlas Connection Example |
| 87 | +------------------------ |
| 88 | + |
| 89 | +To connect to a MongoDB deployment on Atlas, you must first create a client. |
| 90 | + |
| 91 | +You can pass a connection URI as a string to the ``MongoClient.create()`` method |
| 92 | +to connect to a MongoDB instance: |
| 93 | + |
| 94 | +.. literalinclude:: /includes/connect/mongoclient2.kt |
| 95 | + :start-after: start-connect-to-atlas-w-uri |
| 96 | + :end-before: end-connect-to-atlas-w-uri |
| 97 | + :language: kotlin |
| 98 | + :copyable: |
| 99 | + :dedent: |
| 100 | + |
| 101 | +You can also create a client with your desired configurations by passing a |
| 102 | +``MongoClientSettings`` object to the ``MongoClient.create()`` method. |
| 103 | + |
| 104 | +To instantiate a ``MongoClientSettings`` object, use the builder method to |
| 105 | +specify your connection string, using the ``applyConnectionString()`` method, |
| 106 | +and any other client options. Once you have your desired configuration, |
| 107 | +call the ``build()`` method. |
| 108 | + |
| 109 | +You can set the Stable API version client option to avoid breaking changes when |
| 110 | +you upgrade to a new server version. To learn more about the Stable API feature, |
| 111 | +see the :ref:`Stable API page <kotlin-sync-stable-api>`. |
| 112 | + |
| 113 | +The following code shows how you can specify the connection string and the |
| 114 | +Stable API client option when connecting to a MongoDB deployment on Atlas |
| 115 | +and verify that the connection is successful: |
| 116 | + |
| 117 | +.. literalinclude:: /includes/connect/mongoclient.kt |
| 118 | + :start-after: start-connect-to-atlas |
| 119 | + :end-before: end-connect-to-atlas |
| 120 | + :language: kotlin |
| 121 | + :copyable: |
| 122 | + :dedent: |
| 123 | + |
| 124 | +API Documentation |
| 125 | +----------------- |
| 126 | + |
| 127 | +For more information about creating a ``MongoClient`` object with the |
| 128 | +{+driver-short+}, see the following API documentation: |
| 129 | + |
| 130 | +- `MongoClient <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-client/index.html>`__ |
| 131 | +- `MongoClientSettings <{+core-api+}com/mongodb/MongoClientSettings.html>`__ |
0 commit comments