Skip to content

Commit f688b68

Browse files
author
Chris Cho
authored
DOCSP-17791: new local MongoDB connection instructions (#148)
* DOCSP-17791: new local MongoDB connection instructions
1 parent c48b273 commit f688b68

File tree

9 files changed

+91
-57
lines changed

9 files changed

+91
-57
lines changed

source/fundamentals/connection.txt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Connection Guide
33
================
44

55
.. default-domain:: mongodb
6-
6+
77
.. toctree::
88

99
/fundamentals/connection/tls
@@ -60,7 +60,7 @@ connected. The following example shows each part of the connection URI:
6060
:alt: Connection String parts figure
6161

6262
In this example, for the protocol, we use ``mongodb+srv`` which specifies the
63-
:manual:`DNS Seedlist Connection Format </reference/connection-string/#std-label-connections-dns-seedlist>`.
63+
:manual:`DNS Seedlist Connection Format </reference/connection-string/#std-label-connections-dns-seedlist>`.
6464
This indicates that the hostname following it corresponds to the DNS SRV record of your
6565
MongoDB instance or deployment. If your instance or deployment does not have a
6666
DNS SRV record, use ``mongodb`` to specify the :manual:`Standard Connection
@@ -88,21 +88,36 @@ options as parameters. In the following example, we set two connection options:
8888
``retryWrites=true`` and ``w=majority``. For more information on connection
8989
options, skip to the :ref:`connection options <connection-options>` section.
9090

91+
.. _connect-atlas-java-driver:
92+
9193
The following code shows how you can use the sample connection URI in a client to
9294
connect to MongoDB.
9395

9496
.. literalinclude:: /includes/fundamentals/code-snippets/srv.java
9597
:language: java
9698

97-
.. _connect-replica-set:
99+
.. _java-other-ways-to-connect:
98100

99-
Connect to ``localhost``
100-
~~~~~~~~~~~~~~~~~~~~~~~~
101+
Other Ways to Connect to MongoDB
102+
--------------------------------
103+
104+
If you are connecting to a single MongoDB server instance or replica set
105+
that is not hosted on Atlas, see the following sections to find out how to
106+
connect.
107+
108+
Connect to a MongoDB Server on Your Local Machine
109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101110

102-
.. include:: /includes/localhost-connection.rst
111+
.. include:: /includes/fundamentals/localhost-connection.rst
112+
113+
To test whether you can connect to your server, replace the connection
114+
string in the :ref:`Connect to MongoDB Atlas <connect-atlas-java-driver>` code
115+
example and run it.
116+
117+
.. _connect-replica-set:
103118

104119
Connect to a Replica Set
105-
------------------------
120+
~~~~~~~~~~~~~~~~~~~~~~~~
106121

107122
A MongoDB replica set deployment is a group of connected instances that
108123
store the same set of data. This configuration of instances provides data
@@ -151,7 +166,7 @@ Select the tab corresponding to the code snippet you would like to see:
151166
:tabid: mongoclientsettings
152167

153168
.. code-block:: java
154-
169+
155170
ServerAddress seed1 = new ServerAddress("host1", 27017);
156171
ServerAddress seed2 = new ServerAddress("host2", 27017);
157172
ServerAddress seed3 = new ServerAddress("host3", 27017);
@@ -236,11 +251,11 @@ using a method in the ``MongoClientSettings.Builder`` class.
236251
.. tab:: MongoClientSettings
237252
:tabid: mongoclientsettings
238253

239-
To enable compression with
240-
:java-docs:`MongoClientSettings <apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`,
254+
To enable compression with
255+
:java-docs:`MongoClientSettings <apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`,
241256
pass the
242257
:java-docs:`compressorList() <apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#compressorList(java.util.List)>`
243-
builder method a list of
258+
builder method a list of
244259
:java-docs:`MongoCompressor <apidocs/mongodb-driver-core/com/mongodb/MongoCompressor.html>`
245260
instances. You can specify one or more compression algorithms in the list:
246261

@@ -389,7 +404,7 @@ parameters of the connection URI to specify the behavior of the client.
389404
* - **readPreference**
390405
- string
391406
- Specifies the read preference. For more information on values, see
392-
the server documentation for the
407+
the server documentation for the
393408
:manual:`readPreference option </reference/connection-string/#urioption.readPreference>`.
394409

395410
* - **readPreferenceTags**
@@ -467,13 +482,13 @@ parameters of the connection URI to specify the behavior of the client.
467482
- string
468483
- Specifies the UUID representation to use for read and write
469484
operations. For more information, see the the driver documentation
470-
for the
485+
for the
471486
:java-docs:`MongoClientSettings.getUuidRepresentation() method <apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html#getUuidRepresentation()>`.
472487

473488
* - **directConnection**
474489
- boolean
475490
- Specifies that the driver must connect to the host directly.
476491

477-
For a complete list of options, see the
492+
For a complete list of options, see the
478493
:java-docs:`ConnectionString <apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`
479494
API reference page.

source/fundamentals/connection/tls.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ using a method in the ``MongoClientSettings.Builder`` class.
5353
:tabid: mongoclientsettings
5454

5555
To configure your ``MongoClient``'s TLS/SSL connection options using the
56-
``MongoClientSettings.Builder`` class, call the
56+
``MongoClientSettings.Builder`` class, call the
5757
:java-docs:`applyToSslSettings() <apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyToSslSettings(com.mongodb.Block)>`
5858
method. Set the ``enabled`` property to ``true`` in the ``SslSettings.Builder``
5959
block to enable TLS/SSL:
@@ -84,7 +84,7 @@ the following mechanisms:
8484

8585
We based the following sections on the documentation for Oracle JDK,
8686
so some parts may be inapplicable to your JDK or to the custom TLS/SSL
87-
implementation you use.
87+
implementation you use.
8888

8989
.. _tls-configure-jvm-truststore:
9090

@@ -96,8 +96,8 @@ Configure the JVM Trust Store
9696
By default, the JRE includes many commonly used public certificates
9797
from signing authorities like `Let's Encrypt
9898
<https://letsencrypt.org/>`__. As a result, you can connect to
99-
instances of :atlas:`MongoDB Atlas <>` (or any other server whose
100-
certificate is signed by an authority in the JRE's default
99+
instances of :atlas:`MongoDB Atlas </?jmp=docs_driver_java>` (or any other
100+
server whose certificate is signed by an authority in the JRE's default
101101
certificate store) with TLS/SSL without configuring the trust store.
102102

103103
The JVM trust store saves certificates that securely identify other
@@ -168,11 +168,11 @@ You can configure a client-specific trust store and key store using the
168168
``init()`` method of the ``SSLContext`` class.
169169

170170
You can find an example showing how to configure a client with an ``SSLContext``
171-
instance in the
171+
instance in the
172172
:ref:`Customize TLS/SSL Configuration with an SSLContext section of this guide <tls-custom-sslContext>`.
173173

174174
For more information on the ``SSLContext`` class, see the API
175-
documentation for `SSL Context <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/javax/net/ssl/SSLContext.html>`__.
175+
documentation for `SSL Context <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/javax/net/ssl/SSLContext.html>`__.
176176

177177
Disable Hostname Verification
178178
-----------------------------

source/fundamentals/indexes.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ language as an option when creating the index.
243243

244244
.. tip::
245245

246-
Text indexes differ from the more powerful :atlas:`Atlas full text search indexes </atlas-search/>`. Atlas users
247-
should use Atlas search.
246+
Text indexes differ from the more powerful
247+
:atlas:`Atlas full text search indexes </atlas-search/?jmp=docs_driver_java>`.
248+
Atlas users should use Atlas search.
248249

249250
.. common-content-end
250251

@@ -279,19 +280,19 @@ Multiple Fields
279280
A collection can only contain one text index. If you want to create a
280281
text index for multiple text fields, you need to create a compound
281282
index. A text search runs on all the text fields within the compound
282-
index.
283+
index.
283284

284285
The following snippet creates a compound text index for the ``title`` and ``genre``
285286
fields:
286287

287288
.. code-block:: java
288-
289+
289290
collection.createIndex(Indexes.compoundIndex(Indexes.text("title"), Indexes.text("genre")));
290-
291+
291292
For more information, see the following Server Manual Entries:
292293

293294
- :manual:`Compound Text Index Restrictions </core/index-text/#std-label-text-index-compound>`
294-
- :manual:`Text Indexes </core/index-text>`
295+
- :manual:`Text Indexes </core/index-text>`
295296

296297
Geospatial Indexes
297298
~~~~~~~~~~~~~~~~~~
@@ -398,7 +399,7 @@ Remove an Index
398399
---------------
399400

400401
You can remove any unused index except the default unique index on the
401-
``_id`` field.
402+
``_id`` field.
402403

403404
The following sections show the ways to remove indexes:
404405

@@ -412,7 +413,7 @@ Remove an Index Using an Index Specification Document
412413
Pass an **index specification document** to the ``dropIndex()`` method to
413414
remove an index from a collection. An index specification document is
414415
a ``Bson`` instance that specifies the type of index on a
415-
specified field.
416+
specified field.
416417

417418
The following snippet removes an ascending index on the ``title`` field
418419
in a collection:
@@ -425,7 +426,7 @@ in a collection:
425426

426427
If you want to drop a text index, you must use the name of the index
427428
instead. See the :ref:`Remove an Index Using a Name Field
428-
<name_field>` section for details.
429+
<name_field>` section for details.
429430

430431
.. _name_field:
431432

@@ -436,10 +437,10 @@ Pass the ``name`` field of the index to the ``dropIndex()`` method to
436437
remove an index from a collection.
437438

438439
If you need to find the name of your index, use the ``listIndexes()``
439-
method to see the value of the ``name`` fields in your indexes.
440+
method to see the value of the ``name`` fields in your indexes.
440441

441442
The following snippet retrieves and prints all the indexes in a
442-
collection:
443+
collection:
443444

444445
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
445446
:language: java
@@ -448,25 +449,25 @@ collection:
448449
:end-before: end listIndex
449450

450451
If you call ``listIndex()`` on a collection that contains a text index,
451-
the output might resemble the following:
452+
the output might resemble the following:
452453

453454
.. code-block:: json
454455
:copyable: false
455456

456457
{ "v": 2, "key": {"_id": 1}, "name": "_id_" }
457-
{ "v": 2, "key": {"_fts": "text", "_ftsx": 1}, "name": "title_text", "weights": {"title": 1},
458+
{ "v": 2, "key": {"_fts": "text", "_ftsx": 1}, "name": "title_text", "weights": {"title": 1},
458459
"default_language": "english", "language_override": "language", "textIndexVersion": 3 }
459460

460461
This output tells us the names of the existing indexes are "_id" and
461-
"title_text".
462+
"title_text".
462463

463464
The following snippet removes the "title_text" index from the collection:
464465

465466
.. code-block:: java
466467

467468
collection.dropIndex("title_text");
468469

469-
.. note::
470+
.. note::
470471

471472
You cannot remove a single field from a compound text index. You must
472473
drop the entire index and create a new one to update the indexed
@@ -476,7 +477,7 @@ Remove an Index Using a Wildcard Character
476477
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477478

478479
Starting with MongoDB 4.2, you can drop all indexes by calling the
479-
``dropIndexes()`` method on your collection:
480+
``dropIndexes()`` method on your collection:
480481

481482
.. code-block:: java
482483

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
If you need to run a MongoDB server on your local machine for development
2+
purposes instead of using an Atlas cluster, you need to complete the following:
3+
4+
1. Download the `Community <https://www.mongodb.com/try/download/community>`__
5+
or `Enterprise <https://www.mongodb.com/try/download/enterprise>`__ version
6+
of MongoDB Server.
7+
8+
#. `Install and configure <https://docs.mongodb.com/manual/installation/>`__
9+
MongoDB Server.
10+
11+
#. Start the server.
12+
13+
.. important::
14+
15+
Always secure your MongoDB server from malicious attacks. See our
16+
:manual:`Security Checklist </administration/security-checklist/>` for a
17+
list of security recommendations.
18+
19+
After you successfully start your MongoDB server, specify your connection
20+
string in your driver connection code.
21+
22+
If your MongoDB Server is running locally, you can use the connection string
23+
``"mongodb://localhost:<port>"`` where ``<port>`` is the port number you
24+
configured your server to listen for incoming connections.
25+
26+
If you need to specify a different hostname or IP address, see our Server
27+
Manual entry on :manual:`Connection Strings </reference/connection-string/>`.

source/includes/localhost-connection.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

source/includes/quick-start/atlas-setup.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Set up a Free Tier Cluster in Atlas
33

44
After setting up your Java project dependencies, create a MongoDB cluster
55
where you can store and manage your data. Complete the
6-
:atlas:`Get Started with Atlas </getting-started>` guide to set up a new
7-
Atlas account, create and launch a free tier MongoDB cluster, load datasets, and
8-
interact with the data.
6+
:atlas:`Get Started with Atlas </getting-started?jmp=docs_driver_java>` guide
7+
to set up a new Atlas account, create and launch a free tier MongoDB cluster,
8+
load datasets, and interact with the data.
99

1010
After completing the steps in the Atlas guide, you should have a new MongoDB
1111
cluster deployed in Atlas, a new database user, and sample datasets loaded
@@ -24,6 +24,10 @@ includes information on the hostname or IP address and port of your
2424
cluster, authentication mechanism, user credentials when applicable, and
2525
other connection options.
2626

27+
If you are connecting to an instance or cluster that is not hosted by Atlas,
28+
see :ref:`Other Ways to Connect to MongoDB <java-other-ways-to-connect>` for
29+
instructions on how to format your connection string.
30+
2731
To retrieve your connection string for the cluster and user you created in
2832
the previous step, log into your Atlas account and navigate to the
2933
**Clusters** section and click the **Connect** button for the cluster that you

source/quick-start.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ After completing this step, you should have a working application that uses
9696
the Java driver to connect to your MongoDB cluster, run a query on the
9797
sample data, and print out the result.
9898

99-
Connect to ``localhost``
100-
~~~~~~~~~~~~~~~~~~~~~~~~
101-
102-
.. include:: /includes/localhost-connection.rst
103-
10499
Working with POJOs (Optional)
105100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106101

source/usage-examples.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ operations. Each example provides the following information:
4141
How to Use the Usage Examples
4242
-----------------------------
4343

44-
These examples use the :atlas:`sample datasets </sample-data>` provided by
45-
Atlas. You can load them into your database on the free tier of MongoDB
46-
Atlas by following the
47-
:atlas:`Get Started with Atlas Guide </getting-started/#atlas-getting-started>`
44+
These examples use the :atlas:`sample datasets </sample-data?jmp=docs_driver_java>`
45+
provided by Atlas. You can load them into your database on the free tier of
46+
MongoDB Atlas by following the
47+
:atlas:`Get Started with Atlas Guide </getting-started/#atlas-getting-started?jmp=docs_driver_java>`
4848
or you can
4949
:guides:`import the sample dataset into a local MongoDB instance
5050
</server/import/>`.

source/whats-new.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ New features of the 4.3 Java driver release include:
2929
- Added support for connection to
3030
`MongoDB Atlas Serverless Instances <https://www.mongodb.com/cloud/atlas/serverless>`__.
3131
For more information on setup, see our documentation on how to
32-
:atlas:`Create a New Serverless Instance </tutorial/create-new-serverless-instance/>`
32+
:atlas:`Create a New Serverless Instance </tutorial/create-new-serverless-instance/?jmp=docs_driver_java>`
3333
- Added a builder API for the ``setWindowFields`` pipeline stage to allow the use of window operators
3434
- Added support for setting Netty `io.netty.handler.ssl.SslContext <https://netty.io/4.1/api/io/netty/handler/ssl/SslContext.html>`__
3535
- Added support for snapshot reads to ``ClientSession``

0 commit comments

Comments
 (0)