@@ -16,15 +16,7 @@ Get Started with the Scala Driver
1616
1717.. meta::
1818 :description: Learn how to create an app to connect to MongoDB deployment by using the Scala driver.
19- :keywords: quick start, tutorial, basics
20-
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- Connect to MongoDB </get-started/connect-to-mongodb>
27- Next Steps </get-started/next-steps>
19+ :keywords: quick start, tutorial, basics, setup, code example, cloud, host, atlas, uri, test connection, runnable, learn
2820
2921Overview
3022--------
@@ -44,3 +36,264 @@ Follow this guide to connect a sample Scala application to a MongoDB Atlas
4436deployment. If you prefer to connect to MongoDB using a different driver or
4537programming language, see our :driver:`list of official drivers <>`.
4638
39+ .. _scala-quick-start-download-and-install:
40+
41+ Download and Install
42+ --------------------
43+
44+ .. procedure::
45+ :style: connected
46+
47+ .. step:: Install dependencies
48+
49+ Before you being developing, ensure you have the following dependencies
50+ installed in your development environment:
51+
52+ - `JDK <https://www.oracle.com/java/technologies/downloads/>`__ version 8 or later
53+ - `sbt <https://www.scala-sbt.org/1.x/docs/Setup.html>`__ version 1 or later
54+
55+ .. step:: Create a project directory
56+
57+ Run the following command in your shell to create a directory
58+ called ``scala-quickstart`` for this project:
59+
60+ .. code-block:: bash
61+
62+ mkdir scala-quickstart
63+
64+ Select the tab corresponding to your operating system and run the following commands
65+ to create a ``build.sbt`` file in the ``scala-quickstart`` directory:
66+
67+ .. tabs::
68+
69+ .. tab:: macOS / Linux
70+ :tabid: create-file-mac-linux
71+
72+ .. code-block:: bash
73+
74+ cd scala-quickstart
75+ touch build.sbt
76+
77+ .. tab:: Windows
78+ :tabid: create-file-windows
79+
80+ .. code-block:: bash
81+
82+ cd scala-quickstart
83+ type nul > build.sbt
84+
85+ .. step:: Configure your project to use the {+driver-short+}
86+
87+ Navigate to your ``build.sbt`` file and add the following code to use
88+ the {+driver-short+} in your application:
89+
90+ .. code-block:: none
91+
92+ ThisBuild / scalaVersion := "{+language-version+}"
93+ libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "{+full-version+}"
94+
95+ This code configures your application to use Scala version {+language-version+} and
96+ {+driver-short+} version {+full-version+}.
97+
98+ After you complete these steps, you have a new project directory with the driver
99+ dependencies installed.
100+
101+ .. _scala-create-deployment:
102+
103+ Create a MongoDB Deployment
104+ ---------------------------
105+
106+ You can create a free tier MongoDB deployment on MongoDB Atlas
107+ to store and manage your data. MongoDB Atlas hosts and manages
108+ your MongoDB database in the cloud.
109+
110+ .. procedure::
111+ :style: connected
112+
113+ .. step:: Create a free MongoDB deployment on Atlas
114+
115+ Complete the :atlas:`Get Started with Atlas </getting-started>`
116+ guide to set up a new Atlas account and load sample data into a new free
117+ tier MongoDB deployment.
118+
119+ .. step:: Save your credentials
120+
121+ After you create your database user, save that user's
122+ username and password to a safe location for use in an upcoming step.
123+
124+ After you complete these steps, you have a new free tier MongoDB
125+ deployment on Atlas, database user credentials, and sample data loaded
126+ into your database.
127+
128+ .. _scala-connection-string:
129+
130+ Create a Connection String
131+ --------------------------
132+
133+ You can connect to your MongoDB deployment by providing a
134+ **connection URI**, also called a *connection string*, which
135+ instructs the driver on how to connect to a MongoDB deployment
136+ and how to behave while connected.
137+
138+ The connection string includes the hostname or IP address and
139+ port of your deployment, the authentication mechanism, user credentials
140+ when applicable, and connection options.
141+
142+ To learn how to connect to an instance or deployment not hosted on Atlas, see the
143+ :ref:`scala-connection-targets` guide.
144+
145+ .. procedure::
146+ :style: connected
147+
148+ .. step:: Find your MongoDB Atlas Connection String
149+
150+ To retrieve your connection string for the deployment that
151+ you created in the :ref:`previous step <scala-create-deployment>`,
152+ log in to your Atlas account and navigate to the
153+ :guilabel:`Database` section and click the :guilabel:`Connect` button
154+ for your new deployment.
155+
156+ .. figure:: /includes/figures/atlas_connection_select_cluster.png
157+ :alt: The connect button in the clusters section of the Atlas UI
158+
159+ Then, select the :guilabel:`Drivers` option under the :guilabel:`Connect to your application`
160+ header. Select "Scala" from the :guilabel:`Driver` selection
161+ menu and the version that best matches the version you installed
162+ from the :guilabel:`Version` selection menu.
163+
164+ .. step:: Copy your Connection String
165+
166+ Click the button on the right of the connection string to copy it
167+ to your clipboard, as shown in the following screenshot:
168+
169+ .. figure:: /includes/figures/atlas_connection_copy_string_scala.png
170+ :alt: The copy button next to the connection string in the Atlas UI
171+
172+ .. step:: Update the Placeholders
173+
174+ Paste this connection string into a file in your preferred text editor
175+ and replace the ``<db_username>`` and ``<db_password>`` placeholders with
176+ your database user's username and password.
177+
178+ Save this file to a safe location for use in the next step.
179+
180+ After you complete these steps, you have a connection string that
181+ corresponds to your Atlas cluster.
182+
183+ .. _scala-connect-to-mongodb:
184+
185+ Connect to MongoDB
186+ ------------------
187+
188+ After retrieving the connection string for your MongoDB Atlas deployment,
189+ you can connect to the deployment from your Scala application and query
190+ the Atlas sample datasets.
191+
192+ .. procedure::
193+ :style: connected
194+
195+ .. step:: Create an application file
196+
197+ Navigate to the ``scala-quickstart`` directory that you made in the
198+ :ref:`scala-quick-start-download-and-install` step of this guide and
199+ create the ``src/main/scala/quickstart`` nested directories.
200+
201+ Select the tab corresponding to your operating system and run the following
202+ commands to create a ``Main.scala`` file in the ``quickstart`` subdirectory:
203+
204+ .. tabs::
205+
206+ .. tab:: macOS / Linux
207+ :tabid: create-file-mac-linux
208+
209+ .. code-block:: bash
210+
211+ cd src/main/scala/quickstart
212+ touch Main.scala
213+
214+ .. tab:: Windows
215+ :tabid: create-file-windows
216+
217+ .. code-block:: bash
218+
219+ cd src/main/scala/quickstart
220+ type nul > Main.scala
221+
222+ .. step:: Add helper methods
223+
224+ Add the following ``Helpers.scala`` file from the `driver source code
225+ <https://github.com/mongodb/mongo-java-driver/blob/main/driver-scala/src/integration/scala/tour/Helpers.scala>`__
226+ to the ``src/main/scala/quickstart`` directory:
227+
228+ .. literalinclude:: /includes/get-started/Helpers.scala
229+ :language: scala
230+
231+ This file allows you to access helper methods for printing query results.
232+
233+ .. step:: Add your application code
234+
235+ Copy and paste the following code into the ``Main.scala`` file, which queries
236+ the ``movies`` collection in the ``sample_mflix`` database:
237+
238+ .. literalinclude:: /includes/get-started/Main.scala
239+ :language: scala
240+
241+ .. step:: Assign the connection string
242+
243+ Replace the ``<connection string>`` placeholder with the
244+ connection string that you copied from the :ref:`scala-connection-string`
245+ step of this guide.
246+
247+ .. step:: Run your Scala application
248+
249+ In your project root directory, run the following commands to start the sbt shell
250+ and run your application:
251+
252+ .. code-block:: bash
253+
254+ sbt
255+ run
256+
257+ The command line output contains details about the retrieved movie
258+ document:
259+
260+ .. code-block:: none
261+ :copyable: false
262+
263+ {"_id": {"$oid": "..."}, ... , "genres": ["Crime", "Drama"], "rated": "R",
264+ "metacritic": 80, "title": "The Shawshank Redemption", ... }
265+
266+ If you encounter an error or see no output, ensure that you specified the
267+ proper connection string in the ``Main.scala`` file and that you loaded
268+ the sample data.
269+
270+ .. tip::
271+
272+ You can exit the sbt shell by running the following command:
273+
274+ .. code-block:: none
275+
276+ exit
277+
278+ After you complete these steps, you have a Scala application that
279+ connects to your MongoDB deployment, runs a query on the sample
280+ data, and returns a matching document.
281+
282+ .. _scala-quick-start-next-steps:
283+
284+ Next Steps
285+ ----------
286+
287+ Congratulations on completing the quick start tutorial!
288+
289+ In this tutorial, you created a {+language+} application that
290+ connects to a MongoDB deployment hosted on MongoDB Atlas
291+ and retrieves a document that matches a query.
292+
293+ Learn more about {+driver-short+} from the following resources:
294+
295+ - Learn how to perform read operations in the :ref:`<scala-read>` section.
296+
297+ - Learn how to perform write operations in the :ref:`<scala-write>` section.
298+
299+ .. include:: /includes/get-started/troubleshoot.rst
0 commit comments