4
4
Quick Start
5
5
===========
6
6
7
+ .. facet::
8
+ :name: genre
9
+ :values: tutorial
10
+
11
+ .. meta::
12
+ :keywords: code example, get started, runnable app
13
+
7
14
.. contents:: On this page
8
15
:local:
9
16
:backlinks: none
@@ -13,112 +20,146 @@ Quick Start
13
20
Introduction
14
21
------------
15
22
16
- This guide shows you how to create an application that uses the **Kotlin driver**
17
- to connect to a **MongoDB Atlas cluster**. If you prefer to connect to MongoDB
18
- using a different driver or programming language, see our
23
+ This guide shows you how to create an application that uses the **{+ driver-short+} **
24
+ to connect to a **MongoDB Atlas cluster**. If you prefer to connect to
25
+ MongoDB by using a different driver or programming language, see the
19
26
:driver:`list of official MongoDB drivers <>`.
20
27
21
- The Kotlin driver lets you connect to and communicate with MongoDB clusters
28
+ The {+ driver-short+} lets you connect to and communicate with MongoDB clusters
22
29
from a Kotlin application.
23
30
24
- MongoDB Atlas is a fully- managed cloud database service that hosts your data
25
- on MongoDB clusters. In this guide, we show you how to get started with your
26
- own free (no credit card required) cluster.
31
+ MongoDB Atlas is a fully managed cloud database service that hosts your data
32
+ on MongoDB clusters. In this guide, you can learn how to get started with your
33
+ own free cluster.
27
34
28
35
.. tip::
29
36
30
- For an additional example of how to build an application in Kotlin using
31
- MongoDB Atlas and the {+driver-long+} , see the
37
+ To view another example that demonstrates how to build an
38
+ application in Kotlin that connects to MongoDB Atlas , see the
32
39
:website:`Getting Started with the {+driver-long+}
33
- </developer/products/mongodb/getting-started-kotlin-driver/>` developer tutorial.
34
-
35
- Consult the following steps to connect your Kotlin application with a MongoDB Atlas
36
- cluster.
40
+ </developer/products/mongodb/getting-started-kotlin-driver/>`
41
+ developer tutorial.
37
42
38
43
Set up Your Project
39
44
-------------------
40
45
41
46
Install Kotlin
42
47
~~~~~~~~~~~~~~
43
48
44
- Make sure that your system has Kotlin installed running on JDK 1.8 or later.
49
+ Make sure that your system has Kotlin installed and running on JDK 1.8 or later.
45
50
For more information on getting started with Kotlin/JVM development,
46
51
refer to `Get started with Kotlin/JVM <{+kotlin-docs+}/jvm-get-started.html>`__
47
52
in the Kotlin language documentation.
48
53
49
54
Create the Project
50
55
~~~~~~~~~~~~~~~~~~~
51
56
52
- This guide shows you how to add the MongoDB Kotlin driver dependencies using
53
- Gradle or Maven. We recommend that you use an integrated development
54
- environment (IDE) such as Intellij IDEA or Eclipse IDE to make it more convenient
55
- to configure Gradle or Maven to build and run your project.
57
+ This guide shows you how to add the MongoDB Kotlin driver dependencies
58
+ by using Gradle or Maven. We recommend that you use an integrated development
59
+ environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure
60
+ Gradle or Maven to build and run your project.
56
61
57
- If you are not using an IDE, see
58
- `Creating New Gradle Builds <https://guides.gradle.org/creating-new-gradle-builds/>`_
59
- or `Building Maven <https://maven.apache.org/guides/development/guide-building-maven.html>`_
62
+ If you are not using an IDE, see the
63
+ `Creating New Gradle Builds
64
+ <https://guides.gradle.org/creating-new-gradle-builds/>`__ guide
65
+ or the `Building Maven
66
+ <https://maven.apache.org/guides/development/guide-building-maven.html>`__ guide
60
67
for more information on how to set up your project.
61
68
62
69
.. _add-mongodb-dependency:
63
70
64
71
Add MongoDB as a Dependency
65
72
~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
73
67
- If you are using `Gradle <https://gradle.org/>`__, add the following to your
68
- ``build.gradle.kts`` dependencies list:
74
+ If you are using `Gradle <https://gradle.org/>`__ to manage your
75
+ packages, add the following entry to your ``build.gradle.kts``
76
+ dependencies list:
69
77
70
78
.. include:: /includes/kotlin-driver-coroutine-gradle-versioned.rst
71
79
72
- If you are using `Maven <https://maven.apache.org/>`__, add the following to
73
- your ``pom.xml`` dependencies list:
80
+ If you are using `Maven <https://maven.apache.org/>`__ to manage your
81
+ packages, add the following entry to your ``pom.xml`` dependencies list:
74
82
75
83
.. include:: /includes/kotlin-driver-coroutine-maven-versioned.rst
76
84
77
- Once you configure your dependencies, ensure they are available to your
78
- project which may require running your dependency manager and refreshing
79
- the project in your IDE.
85
+ After you configure your dependencies, ensure that they are available to your
86
+ project by running the dependency manager and refreshing the
87
+ project in your IDE.
88
+
89
+ Add Serialization Library Dependencies
90
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
+
92
+ To enable the driver to convert between Kotlin objects and BSON, the
93
+ data format for documents in MongoDB, you must also add one or both of the
94
+ following serialization packages to your application:
95
+
96
+ - ``bson-kotlinx`` *(Recommended)*
97
+ - ``bson-kotlin``
98
+
99
+ If you are using Gradle to manage your packages, add one of the following
100
+ entries to your ``build.gradle.kts`` dependencies list:
101
+
102
+ .. include:: /includes/serialization-libs-gradle-versioned.rst
103
+
104
+ If you are using Maven to manage your packages, add one of the following
105
+ entries to your ``pom.xml`` dependencies list:
106
+
107
+ .. include:: /includes/serialization-libs-maven-versioned.rst
108
+
109
+ After you configure your dependencies, ensure that they are available to your
110
+ project by running the dependency manager and refreshing the
111
+ project in your IDE.
112
+
113
+ To learn more about these packages, see
114
+ :ref:`fundamentals-kotlin-serialization`.
80
115
81
116
Create a MongoDB Cluster
82
117
------------------------
83
118
84
119
After setting up your Kotlin project dependencies, create a MongoDB cluster
85
- where you can store and manage your data. Complete the
86
- :atlas:`Get Started with Atlas </getting-started?jmp=docs_driver_kotlin>` guide
120
+ in which you can store and manage your data. Complete the
121
+ :atlas:`Get Started with Atlas </getting-started?jmp=docs_driver_kotlin>` tutorial
87
122
to set up a new Atlas account, create and launch a free tier MongoDB cluster,
88
- load datasets, and interact with the data .
123
+ and load sample datasets .
89
124
90
- After completing the steps in the Atlas guide, you should have a new MongoDB
91
- cluster deployed in Atlas, a new database user, and sample datasets loaded
92
- into your cluster.
125
+ After you complete the steps in the Get Started with Atlas tutorial, you
126
+ have a new MongoDB cluster deployed in Atlas, a new database user, and
127
+ sample data loaded into your cluster.
93
128
94
129
Connect to your Cluster
95
130
-----------------------
96
131
97
- In this step, we create and run an application that uses the MongoDB Kotlin
98
- driver to connect to your MongoDB cluster and run a query on the sample data.
132
+ This step shows how to create and run an application that uses the
133
+ {+driver-short+} to connect to your MongoDB cluster and run a query on
134
+ the sample data.
99
135
100
- We pass instructions to the driver on how to connect to your
101
- MongoDB cluster in a string called the *connection string*. This string
102
- includes information on the hostname or IP address and port of your
103
- cluster, authentication mechanism, user credentials when applicable , and
104
- other connection options.
136
+ First, you must specify how the driver connects to your MongoDB cluster
137
+ by including a *connection string* in your code . This string includes
138
+ information on the hostname or IP address and port of your cluster,
139
+ authentication mechanism, user credentials, and other connection
140
+ options.
105
141
106
- If you are connecting to an instance or cluster that is not hosted by Atlas,
107
- see :ref:`Other Ways to Connect to MongoDB <kotlin-other-ways-to-connect>` for
142
+ If you are connecting to an instance or cluster that is not hosted on Atlas,
143
+ see the :ref:`Other Ways to Connect to MongoDB
144
+ <kotlin-other-ways-to-connect>` section of the Connection Guide for
108
145
instructions on how to format your connection string.
109
146
110
147
To retrieve your connection string for the cluster and user you created in
111
148
the previous step, log into your Atlas account and navigate to the
112
- :guilabel:`Database` section and click the :guilabel:`Connect` button for the cluster that you
113
- want to connect to as shown below.
149
+ :guilabel:`Database` page under Deployment and click the
150
+ :guilabel:`Connect` button for your cluster, which is shown in the following
151
+ image:
114
152
115
153
.. figure:: /includes/figures/atlas_connection_select_cluster.png
116
154
:alt: Atlas Connection GUI cluster selection screen
117
155
118
- Proceed to the :guilabel:`Connect Your Application` step and select the Kotlin driver.
119
- Select "4.10 or later" for the version.
120
- Click the :guilabel:`Copy` icon to copy the *connection string* to your clipboard as
121
- shown below.
156
+ Select the :guilabel:`Drivers` option for connection and select
157
+ :guilabel:`Kotlin` from the list of drivers and :guilabel:`4.10 or
158
+ later` from the version dropdown.
159
+
160
+ Next, click the :guilabel:`Copy` icon, which is highlighted in the
161
+ following image, to copy your *connection string* to
162
+ your clipboard:
122
163
123
164
.. figure:: /includes/figures/atlas_connection_copy_string_kotlin.png
124
165
:alt: Atlas Connection GUI connection string screen
@@ -129,20 +170,25 @@ for the next step.
129
170
Query Your MongoDB Cluster from Your Application
130
171
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
172
132
- Next, create a file ``QuickStartDataClassExample.kt``. Use the following sample
133
- code to run a query on your sample dataset in MongoDB Atlas, replacing the
134
- value of the ``uri`` variable with your MongoDB Atlas connection string.
135
- The example uses a Kotlin data class to model MongoDB data.
173
+ Next, create a file called ``QuickStartDataClassExample.kt`` in your
174
+ project.
136
175
137
- Ensure you replace the "<password>" section of the connection string with
138
- the password you created for your user that has **atlasAdmin** permissions.
176
+ Copy the following sample code into the file and replace the value of
177
+ the ``uri`` variable with your MongoDB Atlas connection string that you
178
+ saved in the preceding step. Replace the ``"<password>"`` placeholder of
179
+ your connection string with the password you set for your user that has
180
+ **atlasAdmin** permissions:
139
181
140
182
.. literalinclude:: /examples/generated/QuickStartDataClassExample.snippet.quick-start-data-class.kt
141
183
:language: kotlin
142
184
:caption: QuickStartDataClassExample.kt
143
185
144
- When you run the ``main`` function, it should output the details
145
- of the movie from the sample dataset which should look something like this:
186
+ .. note::
187
+
188
+ This example uses a Kotlin data class to model MongoDB data.
189
+
190
+ When you run the ``main`` function, the application prints the details
191
+ of a movie document that matches the query, as shown in the following output:
146
192
147
193
.. code-block:: none
148
194
:copyable: false
@@ -153,26 +199,28 @@ of the movie from the sample dataset which should look something like this:
153
199
cast=[Michael J. Fox, Christopher Lloyd, Lea Thompson, Crispin Glover]
154
200
)
155
201
156
- To learn more about using data classes to store and retrieve data, refer to
157
- :ref:`Document Data Format: Data Classes <fundamentals-data-classes>`.
202
+ .. tip:: Data Classes
203
+
204
+ To learn more about using data classes to store and retrieve data,
205
+ see the :ref:`fundamentals-data-classes` guide.
158
206
159
- If you receive no output or an error, check whether you included the proper
160
- connection string in your Kotlin class, and whether you loaded the sample dataset
161
- into your MongoDB Atlas cluster.
207
+ If you don't see any output or receive an error, check whether you
208
+ included the proper connection string in your application. Also, confirm
209
+ that you successfully loaded the sample dataset into your MongoDB Atlas cluster.
162
210
163
211
.. important:: Known connection issue when using TLS v1.3
164
212
165
- If you encounter an error connecting to your MongoDB instance or cluster
166
- that resembles the following while running your application, you may need
167
- to update your JDK to the latest patch release:
213
+ If you encounter the following error while connecting to your MongoDB
214
+ instance, you must update your JDK to the latest patch release:
168
215
169
216
.. code-block:: none
170
217
:copyable: false
171
218
172
219
javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request
173
220
174
221
This exception is a known issue when using the TLS 1.3 protocol with
175
- specific versions of JDK, but was fixed for the following releases:
222
+ specific versions of JDK, but this issue is fixed for the following
223
+ JDK versions:
176
224
177
225
- JDK 11.0.7
178
226
- JDK 13.0.3
@@ -181,29 +229,28 @@ into your MongoDB Atlas cluster.
181
229
To resolve this error, update your JDK to one of the preceding patch
182
230
versions or a newer one.
183
231
184
- After completing this step, you should have a working application that uses
185
- the Kotlin driver to connect to your MongoDB cluster, run a query on the
232
+ After completing this step, you have a working application that uses
233
+ the {+ driver-short+} to connect to your MongoDB cluster, run a query on the
186
234
sample data, and print out the result.
187
235
188
236
Working with the Document Class (Alternative)
189
237
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
238
191
- In the previous section, you ran a query on a sample collection to retrieve
192
- data with a data class. In this section, you can learn to
193
- use a `Document <{+api+}/apidocs/bson/org/bson/Document.html>`__ class
239
+ The preceding section demonstrates how to run a query on a sample
240
+ collection to retrieve data by using a Kotlin data class. This section
241
+ shows how to use the `Document <{+api+}/apidocs/bson/org/bson/Document.html>`__ class
194
242
to store and retrieve data from MongoDB.
195
243
196
- Next, create a new file to contain your application called ``QuickStartDocumentExample.kt``
197
- in the base package directory of your project. Use the following sample
198
- code to run a query on your sample dataset in MongoDB Atlas, replacing the
199
- value of the ``uri`` variable with your MongoDB Atlas connection string.
244
+ In a new file called ``QuickStartDocumentExample.kt``, paste the following sample
245
+ code to run a query on your sample dataset in MongoDB Atlas. Replace the
246
+ value of the ``uri`` variable with your MongoDB Atlas connection string:
200
247
201
248
.. literalinclude:: /examples/generated/QuickStartDocumentExample.snippet.quick-start-document.kt
202
249
:caption: QuickStartDocumentExample.kt
203
250
:language: kotlin
204
251
205
- When you run the ``main`` function, it should output the details
206
- of the movie from the sample dataset which will look something like this :
252
+ When you run the ``main`` function, the application prints the details
253
+ of a movie document that matches the query, as shown in the following output :
207
254
208
255
.. code-block:: json
209
256
:copyable: false
@@ -217,12 +264,13 @@ of the movie from the sample dataset which will look something like this:
217
264
...
218
265
}
219
266
220
- If you receive no output or an error, check whether you included the proper
221
- connection string in your Kotlin class, and whether you loaded the sample dataset
222
- into your MongoDB Atlas cluster.
267
+ If you don't see any output or receive an error, check whether you
268
+ included the proper connection string in your application. Also, confirm
269
+ that you successfully loaded the sample dataset into your MongoDB Atlas cluster.
223
270
224
271
Next Steps
225
272
----------
226
273
227
- Learn how to read and modify data using the Kotlin driver in the :ref:`Fundamentals
228
- CRUD guide <kotlin-crud-operations>`.
274
+ To learn more about the {+driver-short+}, see the
275
+ :ref:`kotlin-fundamentals-landing` guides, which describe relevant
276
+ concepts in detail and provide code examples for performing different tasks.
0 commit comments