Skip to content

Commit ec8a984

Browse files
DOCSP-27922: connection updates atlas (#338)
* DOCSP-27922: update connection guide to have stableapi * error msg fix * CC PR fixes 1 * Chris feedback * Chris feedback 2 * monospace --------- Co-authored-by: Jordan Smith <[email protected]>
1 parent f461da2 commit ec8a984

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

source/fundamentals/connection/connect.txt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,31 @@ In the example, we set two connection options: ``maxPoolSize=20`` and
7777

7878
.. _connect-atlas-java-driver:
7979

80-
The following code shows how you can use the sample connection URI in a client
81-
to connect to MongoDB.
80+
Atlas Connection Example
81+
------------------------
82+
83+
To connect to a MongoDB deployment on Atlas, create a client. You can
84+
create a client that uses your connection string and other
85+
client options by passing a ``MongoClientSettings`` object to the
86+
``MongoClients.create()`` method.
87+
88+
To instantiate a ``MongoClientSettings`` object, use the builder method to specify
89+
your connection string and any other client options, and then call the ``build()``
90+
method. Chain the ``applyConnectionString()`` method to the builder to specify your
91+
connection URI.
92+
93+
You can set the {+stable-api+} version client option to avoid
94+
breaking changes when you upgrade to a new server version. To
95+
learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
96+
<stable-api-java>`.
97+
98+
The following code shows how you can specify the connection string and
99+
the {+stable-api+} client option when connecting to a MongoDB
100+
deployment on Atlas and verify that the connection is successful:
82101

83102
.. literalinclude:: /includes/fundamentals/code-snippets/srv.java
84103
:language: java
85-
:emphasize-lines: 17
104+
:emphasize-lines: 16
86105

87106
.. _java-other-ways-to-connect:
88107

source/includes/fundamentals/code-snippets/srv.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
package fundamentals;
22

3+
import com.mongodb.*;
34
import org.bson.BsonDocument;
45
import org.bson.BsonInt64;
56
import org.bson.Document;
67
import org.bson.conversions.Bson;
78

8-
import com.mongodb.MongoClientSettings;
9-
import com.mongodb.MongoException;
109
import com.mongodb.client.MongoClient;
1110
import com.mongodb.client.MongoClients;
1211
import com.mongodb.client.MongoDatabase;
1312

1413
public class MongoClientConnectionExample {
1514
public static void main(String[] args) {
16-
// Replace the uri string with your MongoDB deployment's connection string
17-
String uri = "mongodb://user:[email protected]:27017/?maxPoolSize=20&w=majority";
15+
// Replace the placeholders with your credentials and hostname
16+
String uri = "mongodb+srv://<username>:<password>@<hostname>/?retryWrites=true&w=majority";
1817

19-
try (MongoClient mongoClient = MongoClients.create(uri)) {
18+
// Construct a ServerApi instance using the ServerApi.builder() method
19+
ServerApi serverApi = ServerApi.builder()
20+
.version(ServerApiVersion.V1)
21+
.build();
2022

21-
MongoDatabase database = mongoClient.getDatabase("admin");
23+
MongoClientSettings settings = MongoClientSettings.builder()
24+
.applyConnectionString(new ConnectionString(uri))
25+
.serverApi(serverApi)
26+
.build();
2227

28+
// Create a new client and connect to the server
29+
try (MongoClient mongoClient = MongoClients.create(settings)) {
30+
MongoDatabase database = mongoClient.getDatabase("admin");
2331
try {
32+
// Send a ping to confirm a successful connection
2433
Bson command = new BsonDocument("ping", new BsonInt64(1));
2534
Document commandResult = database.runCommand(command);
26-
System.out.println("Connected successfully to server.");
35+
System.out.println("Pinged your deployment. You successfully connected to MongoDB!");
2736
} catch (MongoException me) {
28-
System.err.println("An error occurred while attempting to run a command: " + me);
37+
System.err.println(me);
2938
}
3039
}
3140
}

0 commit comments

Comments
 (0)