@@ -19,8 +19,6 @@ Get Started with the Java Driver
19
19
Create a Connection String </get-started/connection-string/>
20
20
Connect to MongoDB </get-started/connect-to-mongodb/>
21
21
Next Steps </get-started/next-steps/>
22
- Databases & Collections </get-started/databases-collections/>
23
- Integrations </get-started/integrations/>
24
22
25
23
Overview
26
24
--------
@@ -49,4 +47,212 @@ deployment. The tutorial includes the following sections:
49
47
that connects to MongoDB and queries data stored in your deployment.
50
48
51
49
If you prefer to connect to MongoDB using a different driver or
52
- programming language, see our :driver:`list of official drivers <>`.
50
+ programming language, see our :driver:`list of official drivers <>`.
51
+
52
+ .. _java-get-started-download-and-install:
53
+
54
+ Download and Install
55
+ --------------------
56
+
57
+ Complete the following steps to install the {+driver-short+} and
58
+ its dependencies in your development environment.
59
+
60
+ .. procedure::
61
+ :style: connected
62
+
63
+ .. step:: Install the driver dependencies
64
+
65
+ Before you begin this tutorial, ensure that you install
66
+ the following dependencies:
67
+
68
+ - `JDK <https://www.oracle.com/java/technologies/javase-downloads.html>`__
69
+ version 8 or later
70
+ - Integrated development environment (IDE), such as `IntelliJ IDEA <https://www.jetbrains.com/idea/download/>`__
71
+ or `Eclipse <https://www.eclipse.org/downloads/packages/>`__
72
+
73
+ .. note::
74
+
75
+ This tutorial shows how to install the {+driver-short+} by using
76
+ Maven or Gradle in an IDE. If you do not use an IDE, visit `Building Maven
77
+ <https://maven.apache.org/guides/development/guide-building-maven.html>`__
78
+ or `Creating New Gradle Builds <https://guides.gradle.org/creating-new-gradle-builds/>`__
79
+ to learn how to set up your project.
80
+
81
+ .. step:: Install the {+driver-short+}
82
+
83
+ In your IDE, create a new `Maven <https://maven.apache.org/>`__ or `Gradle <https://gradle.org/>`__
84
+ project. If you use Maven, add the following code to your ``pom.xml`` dependencies list:
85
+
86
+ .. code-block:: xml
87
+
88
+ <dependencies>
89
+ <dependency>
90
+ <groupId>org.mongodb</groupId>
91
+ <artifactId>mongodb-driver-sync</artifactId>
92
+ <version>{+full-version+}</version>
93
+ </dependency>
94
+ </dependencies>
95
+
96
+ If you use Gradle, add the following code to your ``build.gradle`` dependencies list:
97
+
98
+ .. code-block:: groovy
99
+
100
+ dependencies {
101
+ implementation 'org.mongodb:mongodb-driver-sync:{+full-version+}'
102
+ }
103
+
104
+ After you configure your dependencies, ensure they are available to your
105
+ project by running your dependency manager and refreshing
106
+ the project in your IDE.
107
+
108
+ After you complete these steps, you have a new project
109
+ and the driver dependencies installed.
110
+
111
+ .. _java-get-started-create-deployment:
112
+
113
+ Create a MongoDB Deployment
114
+ ---------------------------
115
+
116
+ You can create a free tier MongoDB deployment on MongoDB Atlas
117
+ to store and manage your data. MongoDB Atlas hosts and manages
118
+ your MongoDB database in the cloud.
119
+
120
+ .. procedure::
121
+ :style: connected
122
+
123
+ .. step:: Create a free MongoDB deployment on Atlas
124
+
125
+ Complete the :atlas:`Get Started with Atlas </getting-started?tck=docs_driver_java>`
126
+ guide to set up a new Atlas account and a free tier MongoDB deployment.
127
+ Ensure that you :atlas:`load sample data </sample-data/>` and
128
+ :atlas:`add your IP address </security/add-ip-address-to-list/>` to the IP access
129
+ list.
130
+
131
+ .. step:: Save your credentials
132
+
133
+ After you create your database user, save that user's
134
+ username and password to a safe location for use in an upcoming step.
135
+
136
+ After you complete these steps, you have a new free tier MongoDB
137
+ deployment on Atlas, database user credentials, and sample data loaded
138
+ in your database.
139
+
140
+ .. _java-get-started-connection-string:
141
+
142
+ Create a Connection String
143
+ --------------------------
144
+
145
+ You can connect to your MongoDB deployment by providing a
146
+ **connection URI**, also called a *connection string*, which
147
+ instructs the driver on how to connect to a MongoDB deployment
148
+ and how to behave while connected.
149
+
150
+ The connection string includes the hostname or IP address and
151
+ port of your deployment, the authentication mechanism, user credentials
152
+ when applicable, and connection options.
153
+
154
+ .. procedure::
155
+ :style: connected
156
+
157
+ .. step:: Find your MongoDB Atlas connection string
158
+
159
+ To retrieve your connection string for the deployment that
160
+ you created in the :ref:`previous step <java-get-started-create-deployment>`,
161
+ log into your Atlas account and navigate to the
162
+ :guilabel:`Clusters` section. Click the :guilabel:`Connect` button
163
+ for your new deployment, as shown in the following screenshot:
164
+
165
+ .. figure:: /includes/figures/atlas_connection_connect_cluster.png
166
+ :alt: The connect button in the clusters section of the Atlas UI
167
+
168
+ Then, proceed to the :guilabel:`Connect your application` section. Select
169
+ "Java" from the :guilabel:`Driver` selection menu and the version
170
+ that best matches the version you installed from the :guilabel:`Version`
171
+ selection menu.
172
+
173
+ .. step:: Copy your connection string
174
+
175
+ Click the button on the right of the connection string to copy it to
176
+ your clipboard, as shown in the following screenshot:
177
+
178
+ .. figure:: /includes/figures/atlas_connection_copy_uri_java.png
179
+ :alt: The connection string copy button in the Atlas UI
180
+
181
+ .. step:: Edit your connection string placeholders
182
+
183
+ Paste your connection string into a file in your preferred text editor
184
+ and save this file to a safe location for later use. In your connection
185
+ string, replace the ``<db_username>`` and ``<db_password>`` placeholders with
186
+ your database user's username and password.
187
+
188
+ After completing these steps, you have a connection string that
189
+ contains your database username and password.
190
+
191
+ Run a Sample Query
192
+ ------------------
193
+
194
+ .. facet::
195
+ :name: genre
196
+ :values: tutorial
197
+
198
+ .. meta::
199
+ :keywords: test connection, runnable, code example
200
+
201
+ After retrieving the connection string for your MongoDB Atlas deployment,
202
+ you can connect to the deployment from your Java application and query
203
+ the Atlas sample datasets.
204
+
205
+ .. procedure::
206
+ :style: connected
207
+
208
+ .. step:: Create your Java application file
209
+
210
+ In your project's base package directory, create a file called
211
+ ``QuickStart.java``. Copy and paste the following code into this file,
212
+ which queries the ``movies`` collection in the ``sample_mflix`` database:
213
+
214
+ .. literalinclude:: /includes/get-started/code-snippets/QuickStart.java
215
+ :start-after: begin QuickStart
216
+ :end-before: end QuickStart
217
+ :language: java
218
+ :dedent:
219
+
220
+ .. step:: Assign the connection string
221
+
222
+ Replace the ``<connection string uri>`` placeholder with the connection string
223
+ that you copied from the :ref:`java-get-started-connection-string` step of this
224
+ guide.
225
+
226
+ .. step:: Run your Java application
227
+
228
+ Run your application in your IDE or your shell. Your output
229
+ contains details about the retrieved movie document:
230
+
231
+ .. include:: /includes/get-started/query-output.rst
232
+
233
+ .. include:: /includes/get-started/jdk-tls-issue.rst
234
+
235
+ After you complete these steps, you have a Java application that
236
+ connects to your MongoDB deployment, runs a query on the sample
237
+ data, and returns a matching document.
238
+
239
+ .. _java-get-started-next-steps:
240
+
241
+ Next Steps
242
+ ----------
243
+
244
+ Congratulations on completing the tutorial!
245
+
246
+ .. include:: /includes/get-started/quickstart-troubleshoot.rst
247
+
248
+ In this tutorial, you created a {+driver-short+} application that
249
+ connects to a MongoDB deployment hosted on MongoDB Atlas
250
+ and retrieves a document that matches a query.
251
+
252
+ You can continue to develop your sample application by
253
+ visiting the following guides:
254
+
255
+ - :ref:`java-db-coll`: Learn more about interacting with
256
+ MongoDB databases and collections.
257
+ - :ref:`java-integrations`: Learn about the third-party
258
+ integrations that you can use with the {+driver-short+}.
0 commit comments