Skip to content

Commit ce38b14

Browse files
authored
[Kotlin] Duplicate Titles - Connect to MongoDB (#84)
* get started merge * bom
1 parent 5aa4e8b commit ce38b14

8 files changed

+281
-297
lines changed

snooty.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ intersphinx = [
99

1010
toc_landing_pages = [
1111
"/write-operations",
12-
"/get-started",
1312
"/read",
1413
"/connect",
1514
"/indexes",

source/get-started.txt

+280-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ Get Started with the Kotlin Sync Driver
1818
:description: Learn how to create an app to connect to MongoDB deployment by using the Kotlin Sync driver.
1919
:keywords: quick start, tutorial, basics
2020

21-
.. toctree::
22-
23-
Download & Install </get-started/download-and-install/>
24-
Create a Deployment </get-started/create-a-deployment/>
25-
Create a Connection String </get-started/create-a-connection-string/>
26-
Run a Sample Query </get-started/run-sample-query/>
27-
Next Steps </get-started/next-steps/>
28-
2921
Overview
3022
--------
3123

@@ -43,3 +35,283 @@ MongoDB Atlas and interact with data.
4335
Follow this guide to connect a sample {+language+} application to a MongoDB Atlas
4436
deployment. If you prefer to connect to MongoDB using a different driver or
4537
programming language, see the :driver:`list of official MongoDB drivers <>`.
38+
39+
.. _kotlin-sync-download-install:
40+
41+
Download and Install
42+
--------------------
43+
44+
This section demonstrates how to create a project and add the
45+
{+driver-short+} dependencies by using `Gradle <https://gradle.org/>`__
46+
or `Maven <https://maven.apache.org/>`__.
47+
48+
.. procedure::
49+
:style: connected
50+
51+
.. step:: Create a {+language+} Project
52+
53+
First, make sure that your system has {+language+} installed and
54+
running on JDK 1.8 or later.
55+
56+
We recommend that you use an integrated development
57+
environment (IDE) such as IntelliJ IDEA or Eclipse IDE to
58+
configure Gradle or Maven to build and run your project.
59+
60+
.. tip::
61+
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
67+
for more information on how to set up your project.
68+
69+
For more information on getting started with
70+
{+language+} and creating your first project, see `Get started with Kotlin/JVM
71+
<{+kotlin-docs+}/docs/jvm-get-started.html>`__ in the {+language+}
72+
language documentation.
73+
74+
.. step:: Add the Driver Bill of Materials
75+
76+
.. _kotlin-sync-get-started-install-bom:
77+
78+
.. sharedinclude:: dbx/jvm/bom.rst
79+
80+
.. replacement:: gradle-filename
81+
82+
``build.gradle.kts``
83+
84+
.. step:: Add MongoDB as a Dependency
85+
86+
If you are using Gradle to manage your
87+
packages, add the following entry to your ``build.gradle.kts``
88+
dependencies list:
89+
90+
.. include:: /includes/kotlin-sync-driver-gradle-versioned.rst
91+
92+
If you are using Maven to manage your
93+
packages, add the following entry to your ``pom.xml`` dependencies list:
94+
95+
.. include:: /includes/kotlin-sync-driver-maven-versioned.rst
96+
97+
Because you installed the BOM, you can omit a version in the
98+
{+driver-short+} dependency entry. The version you specify in the
99+
BOM determines the dependency versions to install.
100+
101+
After you configure your dependencies, ensure that they are
102+
available to your project by running the dependency manager and
103+
refreshing the project in your IDE.
104+
105+
.. step:: Add Serialization Library Dependencies
106+
107+
To enable the driver to convert between {+language+} objects and BSON, the
108+
data format for documents in MongoDB, you must also add one or both of the
109+
following serialization packages to your application:
110+
111+
- ``bson-kotlinx`` *(Recommended)*
112+
- ``bson-kotlin``
113+
114+
If you are using Gradle to manage your packages, add one of the following
115+
entries to your ``build.gradle.kts`` dependencies list:
116+
117+
.. include:: /includes/serialization-libs-gradle-versioned.rst
118+
119+
If you are using Maven to manage your packages, add one of the following
120+
entries to your ``pom.xml`` dependencies list:
121+
122+
.. include:: /includes/serialization-libs-maven-versioned.rst
123+
124+
After you configure your dependencies, ensure that they are available to your
125+
project by running the dependency manager and refreshing the
126+
project in your IDE.
127+
128+
To learn more about these packages, see :ref:`kotlin-sync-serialization`.
129+
130+
After you complete these steps, you have a new project directory
131+
and the driver dependencies installed.
132+
133+
.. _kotlin-sync-get-started-create-deployment:
134+
135+
Create a MongoDB Deployment
136+
---------------------------
137+
138+
You can create a free tier MongoDB deployment on MongoDB Atlas
139+
to store and manage your data. MongoDB Atlas hosts and manages
140+
your MongoDB database in the cloud.
141+
142+
.. procedure::
143+
:style: connected
144+
145+
.. step:: Create a Free MongoDB deployment on Atlas
146+
147+
Complete the :atlas:`Get Started with Atlas </getting-started>`
148+
guide to set up a new Atlas account and load sample data into a new free
149+
tier MongoDB deployment.
150+
151+
.. step:: Save your Credentials
152+
153+
After you create your database user, save the user's
154+
username and password to a safe location for use in an upcoming step.
155+
156+
After you complete these steps, you have a new free tier MongoDB
157+
deployment on Atlas, database user credentials, and sample data loaded
158+
in your database.
159+
160+
.. _kotlin-sync-get-started-connection-string:
161+
162+
Create a Connection String
163+
--------------------------
164+
165+
You can connect to your MongoDB deployment by providing a
166+
**connection URI**, also called a *connection string*, which
167+
instructs the driver on how to connect to a MongoDB deployment
168+
and how to behave while connected.
169+
170+
The connection string includes the hostname or IP address and
171+
port of your deployment, the authentication mechanism, user credentials
172+
when applicable, and connection options.
173+
174+
To connect to an instance or deployment not hosted on Atlas, see the :ref:`kotlin-sync-connection-targets` guide.
175+
176+
.. procedure::
177+
:style: connected
178+
179+
.. step:: Find your MongoDB Atlas Connection String
180+
181+
To retrieve your connection string for the deployment that
182+
you created in the :ref:`previous step <kotlin-sync-get-started-create-deployment>`,
183+
log into your Atlas account, navigate to the
184+
:guilabel:`Database` section, then click the :guilabel:`Connect` button
185+
for your new deployment.
186+
187+
.. figure:: /includes/figures/atlas_connection_select_cluster.png
188+
:alt: The connect button in the clusters section of the Atlas UI
189+
190+
Proceed to the :guilabel:`Connect your application` section, then select
191+
**{+language+}** from the :guilabel:`Driver` selection menu.
192+
193+
Select the :guilabel:`Password (SCRAM)` authentication mechanism.
194+
195+
Deselect the :guilabel:`Include full driver code example` option to view
196+
only the connection string.
197+
198+
.. step:: Copy your Connection String
199+
200+
Click the button on the right of the connection string to copy it to
201+
your clipboard as shown in the following screenshot:
202+
203+
.. figure:: /includes/figures/atlas_connection_copy_string_kotlin.png
204+
:alt: The connection string copy button in the Atlas UI
205+
206+
.. step:: Update the Placeholders
207+
208+
Paste this connection string into a file in your preferred text editor
209+
and replace the ``<username>`` and ``<password>`` placeholders with
210+
your database user's username and password.
211+
212+
Save this file to a safe location to use in the next step.
213+
214+
After completing these steps, you have a connection string that
215+
contains your database username and password.
216+
217+
.. _kotlin-sync-connect-to-mongodb:
218+
.. _kotlin-sync-run-sample-query:
219+
220+
Run a Sample Query
221+
------------------
222+
223+
.. procedure::
224+
:style: connected
225+
226+
.. step:: Create the Application File
227+
228+
Create a file called ``DemoDataClassExample.kt`` in your project.
229+
230+
Copy the following sample code into the file and replace the value of
231+
the ``<connection URI string>`` placeholder with your MongoDB
232+
Atlas connection string that you saved in the preceding step.
233+
234+
.. literalinclude:: /includes/get-started/DemoDataClassExample.kt
235+
:language: kotlin
236+
:caption: DemoDataClassExample.kt
237+
238+
.. note::
239+
240+
This example uses a {+language+} data class to model MongoDB data.
241+
242+
.. step:: Run the Application
243+
244+
When you run the application, it prints the details
245+
of a movie document that matches the query, as shown in the
246+
following output:
247+
248+
.. code-block:: none
249+
:copyable: false
250+
251+
Movie(title=Before Sunrise, year=1995, directors=[Richard Linklater])
252+
253+
If you don't see any output or receive an error, check whether you
254+
included the proper connection string in your application. Also, confirm
255+
that you successfully loaded the sample dataset into your MongoDB Atlas cluster.
256+
257+
After completing this step, you have a working application that uses
258+
the {+driver-short+} to connect to your MongoDB cluster, run a query on the
259+
sample data, and print out the result.
260+
261+
.. step:: Use the Document Class to Model Data (Alternative)
262+
263+
The preceding step demonstrates how to run a query on a sample
264+
collection to retrieve data by using a {+language+} data class. This section
265+
shows how to use the `Document <https://mongodb.github.io/mongo-java-driver/{+version-number+}/apidocs/bson/org/bson/Document.html>`__
266+
class to store and retrieve data from MongoDB.
267+
268+
In a file called ``DemoDocumentExample.kt``, paste the following sample
269+
code to run a query on your sample dataset in MongoDB Atlas. Replace the
270+
value of the ``<connection URI string>`` placeholder with your
271+
MongoDB Atlas connection string:
272+
273+
.. literalinclude:: /includes/get-started/DemoDocumentExample.kt
274+
:caption: DemoDocumentExample.kt
275+
:language: kotlin
276+
277+
When you run the application, it prints the details
278+
of a movie document that matches the query, as shown in the
279+
following output:
280+
281+
.. code-block:: none
282+
:copyable: false
283+
284+
Document{{_id=..., plot=A young man and woman ..., genres=[Drama, Romance], ...}}
285+
286+
If you don't see any output or receive an error, check whether you
287+
included the proper connection string in your application. Also, confirm
288+
that you successfully loaded the sample dataset into your MongoDB
289+
Atlas cluster.
290+
291+
After you complete these steps, you have a working application that
292+
uses the driver to connect to your MongoDB deployment, runs a query on
293+
the sample data, and prints out the result.
294+
295+
.. TODO add after output .. tip:: Data Classes
296+
..
297+
.. To learn more about using data classes to store and retrieve data,
298+
.. see the :ref:`fundamentals-data-classes` guide.
299+
300+
.. _kotlin-sync-get-started-next-steps:
301+
302+
Next Steps
303+
----------
304+
305+
Congratulations on completing the tutorial!
306+
307+
.. include:: /includes/get-started/quickstart-troubleshoot.rst
308+
309+
In this tutorial, you created a {+language+} application that
310+
connects to a MongoDB deployment hosted on MongoDB Atlas
311+
and retrieves a document that matches a query.
312+
313+
Learn more about the {+driver-short+} from the following resources:
314+
315+
- Learn how to perform read operations in the :ref:`<kotlin-sync-read>` section.
316+
317+
- Learn how to perform write operations in the :ref:`<kotlin-sync-write>` section.

source/get-started/create-a-connection-string.txt

-59
This file was deleted.

source/get-started/create-a-deployment.txt

-30
This file was deleted.

0 commit comments

Comments
 (0)