Skip to content

Commit ab30e3d

Browse files
committed
new option for connecting
1 parent c70619f commit ab30e3d

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

source/connect/mongoclient.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ To connect to a MongoDB deployment, you need two things:
2424

2525
- A **connection URI**, also known as a *connection string*, which tells the {+driver-short+}
2626
which MongoDB deployment to connect to.
27-
- A **MongoClient** object, which creates the connection to and perform
27+
- A **MongoClient** object, which creates the connection to and performs
2828
operations on the MongoDB deployment.
2929

30-
You can also use either of these components to customize the way the {+driver-short+} behaves
30+
You can also use ``MongoClientSettings`` to customize the way the {+driver-short+} behaves
3131
while connected to MongoDB.
3232

3333
This guide shows you how to create a connection string and use a ``MongoClient`` object
@@ -40,8 +40,8 @@ Connection URI
4040

4141
A standard connection string includes the following components:
4242

43-
.. TODO, add this as last sentence for ``username:password`` description: For more information about the ``authSource``
44-
.. connection option, see :ref:`kotlin-sync-auth`.
43+
.. TODO, add this as last sentence for ``username:password`` description once a kotlin auth page is made:
44+
.. For more information about the ``authSource`` connection option, see :ref:`kotlin-sync-auth`.
4545

4646
.. list-table::
4747
:widths: 20 80
@@ -87,7 +87,18 @@ Atlas Connection Example
8787
------------------------
8888

8989
To connect to a MongoDB deployment on Atlas, you must first create a client.
90-
You can create a client with your desired configurations by passing a
90+
91+
You can pass a connection URI as a string to ``MongoClient.create()`` to connect
92+
to a MongoDB instance:
93+
94+
.. literalinclude:: /includes/connect/mongoclient2.kt
95+
:start-after: start-connect-to-atlas-w-uri
96+
:end-before: end-connect-to-atlas-w-uri
97+
:language: kotlin
98+
:copyable:
99+
:dedent:
100+
101+
You can also create a client with your desired configurations by passing a
91102
``MongoClientSettings`` object to the ``MongoClient.create()`` method.
92103

93104
To instantiate a ``MongoClientSettings`` object, use the builder method to

source/includes/connect/mongoclient.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import org.bson.Document
66
fun main() {
77

88
// start-connect-to-atlas
9+
// start-connect-to-atlas-w-uri
910
// Replace the placeholder with your Atlas connection string
1011
val uri = "<connection string>"
11-
12+
val mongoClient1 = MongoClient.create(uri)
13+
// end-connect-to-atlas-w-uri
14+
1215
// Construct a ServerApi instance using the ServerApi.builder() method
1316
val serverApi = ServerApi.builder()
1417
.version(ServerApiVersion.V1)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import com.mongodb.*
2+
import com.mongodb.kotlin.client.MongoClient
3+
4+
fun main() {
5+
6+
// start-connect-to-atlas-w-uri
7+
// Replace the placeholder with your Atlas connection string
8+
val uri = "<connection string>"
9+
10+
// Create a new client and connect to the server
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_mflix")
13+
// end-connect-to-atlas-w-uri
14+
15+
try {
16+
// Send a ping to confirm a successful connection
17+
val command = Document("ping", BsonInt64(1))
18+
val commandResult = database.runCommand(command)
19+
println("Pinged your deployment. You successfully connected to MongoDB!")
20+
} catch (me: MongoException) {
21+
System.err.println(me)
22+
}
23+
}

0 commit comments

Comments
 (0)