Skip to content

Commit 7e0b5ea

Browse files
authored
Merge branch 'master' into DOCSP-41115-get-started
2 parents 6e4196a + 0379473 commit 7e0b5ea

29 files changed

+2628
-4
lines changed

snooty.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
77
]
88

99
toc_landing_pages = [
10+
"/get-started",
1011
"/read",
11-
"/get-started"
12+
"/connect",
13+
"/indexes",
14+
"work-with-indexes",
1215
]
1316

17+
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
18+
1419
[constants]
1520
driver-long = "MongoDB Kotlin Sync Driver"
1621
driver-short = "Kotlin Sync driver"
@@ -21,5 +26,5 @@ version = "v{+version-number+}"
2126
mdb-server = "MongoDB Server"
2227
stable-api = "Stable API"
2328
api = "https://mongodb.github.io/mongo-java-driver/{+version-number+}/apidocs/mongodb-driver-kotlin-sync"
24-
core-api = "https://mongodb.github.io/mongo-java-driver/5.1/apidocs"
2529
kotlin-docs = "https://kotlinlang.org"
30+
core-api = "https://mongodb.github.io/mongo-java-driver/{+version-number+}/apidocs/mongodb-driver-core/"

source/connect.txt

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
.. _kotlin-sync-connect:
2+
3+
==================
4+
Connect to MongoDB
5+
==================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to use the Kotlin Sync driver to connect to MongoDB.
19+
:keywords: client, ssl, tls, localhost
20+
21+
.. .. toctree::
22+
.. :titlesonly:
23+
.. :maxdepth: 1
24+
..
25+
.. /connect/mongoclient
26+
.. /connect/connection-targets
27+
.. /connect/connection-options
28+
.. /connect/tls
29+
.. /connect/network-compression
30+
.. /connect/server-selection
31+
.. /connect/stable-api
32+
.. /connect/csot
33+
34+
Overview
35+
--------
36+
37+
This page contains code examples that show how to use the
38+
{+driver-short+} to connect your application to MongoDB by specifying
39+
various settings.
40+
41+
.. .. tip::
42+
..
43+
.. To learn more about the connection options on this page, see the link
44+
.. provided in each section.
45+
46+
To use a connection example from this page, copy the code example into the
47+
:ref:`sample application <kotlin-sync-connect-sample>` or your own application.
48+
Be sure to replace all placeholders in the code examples, such as
49+
``<hostname>``, with the relevant values for your MongoDB deployment.
50+
51+
.. _kotlin-sync-connect-sample:
52+
53+
.. include:: /includes/usage-examples/sample-app-intro.rst
54+
55+
.. literalinclude:: /includes/usage-examples/connect-sample-app.kt
56+
:language: python
57+
:copyable: true
58+
:linenos:
59+
:emphasize-lines: 6-8
60+
61+
Connection
62+
----------
63+
64+
The following sections describe how to connect to different targets,
65+
such as a local instance of MongoDB or a cloud-hosted instance on Atlas.
66+
67+
Local Deployment
68+
~~~~~~~~~~~~~~~~
69+
70+
The following code shows the connection string to connect to a local
71+
instance of MongoDB:
72+
73+
.. code-block:: kotlin
74+
75+
val uri = "mongodb://localhost:27017/"
76+
val mongoClient = MongoClient.create(uri)
77+
78+
Atlas
79+
~~~~~
80+
81+
The following code shows the connection string to connect to a
82+
deployment hosted on Atlas:
83+
84+
.. code-block:: kotlin
85+
86+
val uri = "mongodb+srv://<username>:<password>@<hostname/port>/?<options>"
87+
val mongoClient = MongoClient.create(uri)
88+
89+
Replica Set
90+
~~~~~~~~~~~
91+
92+
The following code shows the connection string to connect to a
93+
replica set:
94+
95+
.. code-block:: kotlin
96+
97+
val uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
98+
val mongoClient = MongoClient.create(uri)
99+
100+
Transport Layer Security (TLS)
101+
------------------------------
102+
103+
The following sections describe how to connect to MongoDB
104+
while enabling the TLS protocol.
105+
106+
Enable TLS
107+
~~~~~~~~~~
108+
109+
The following tabs demonstrate how to enable TLS on a connection:
110+
111+
.. include:: /includes/connect/tls-tabs.rst
112+
113+
.. To learn more about enabling TLS, see :ref:`kotlin-sync-enable-tls` in
114+
.. the TLS configuration guide.
115+
116+
Disable Hostname Verification
117+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118+
119+
The following tabs demonstrate how to disable hostname verification when
120+
connecting by using TLS:
121+
122+
.. include:: /includes/connect/disable-host-verification-tabs.rst
123+
124+
.. To learn more about disabling hostname verification, see :ref:`kotlin-sync-insecure-tls` in
125+
.. the TLS configuration guide.
126+
127+
Network Compression
128+
-------------------
129+
130+
The following sections describe how to connect to MongoDB
131+
while specifying network compression algorithms.
132+
133+
Compression Algorithms
134+
~~~~~~~~~~~~~~~~~~~~~~
135+
136+
The following tabs demonstrate how to specify all available compressors
137+
while connecting to MongoDB:
138+
139+
.. include:: /includes/connect/compression-tabs.rst
140+
141+
.. To learn more about specifying compression algorithms, see
142+
.. :ref:`kotlin-sync-enable-compression` in the Network Compression guide.
143+
144+
zlib Compression Level
145+
~~~~~~~~~~~~~~~~~~~~~~
146+
147+
The following tabs demonstrate how to specify a compression level for
148+
the ``zlib`` compressor:
149+
150+
.. include:: /includes/connect/zlib-level-tabs.rst
151+
152+
.. To learn more about setting the zlib compression level, see
153+
.. :ref:`kotlin-sync-enable-compression` in the Network Compression guide.
154+
155+
Server Selection
156+
----------------
157+
158+
The following code shows a connection string that specifies a server
159+
selection function:
160+
161+
.. code-block:: kotlin
162+
163+
val client = MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>",
164+
server_selector=<selector function>)
165+
166+
.. To learn more about customizing server selection, see
167+
.. :ref:`kotlin-sync-server-selection`.
168+
169+
{+stable-api+}
170+
--------------
171+
172+
The following code shows how to specify Stable API settings within a
173+
``MongoClientSettings`` instance:
174+
175+
.. code-block:: kotlin
176+
177+
val serverApi = ServerApi.builder()
178+
.version(ServerApiVersion.V1)
179+
.build()
180+
181+
val uri = "<connection string>"
182+
183+
val settings = MongoClientSettings.builder()
184+
.applyConnectionString(ConnectionString(uri))
185+
.serverApi(serverApi)
186+
.build()
187+
188+
val client = MongoClient.create(settings)
189+
190+
.. To learn more about the {+stable-api+}, see :ref:`kotlin-sync-stable-api`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. tabs::
2+
3+
.. tab:: MongoClient
4+
:tabid: mongoclient
5+
6+
.. code-block:: kotlin
7+
8+
val settings = MongoClientSettings.builder()
9+
.applyConnectionString(ConnectionString("<connection string>"))
10+
.compressorList(
11+
listOf(
12+
MongoCompressor.createSnappyCompressor(),
13+
MongoCompressor.createZlibCompressor(),
14+
MongoCompressor.createZstdCompressor())
15+
)
16+
.build()
17+
18+
val mongoClient = MongoClient.create(settings)
19+
20+
.. tab:: Connection String
21+
:tabid: connectionstring
22+
23+
.. code-block:: kotlin
24+
25+
val uri = ConnectionString("mongodb+srv://<user>:<password>@<cluster-url>/?compressors=snappy,zlib,zstd")
26+
27+
val mongoClient = MongoClient.create(uri)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. tabs::
2+
3+
.. tab:: MongoClient
4+
:tabid: mongoclient
5+
6+
.. code-block:: kotlin
7+
8+
val settings = MongoClientSettings.builder()
9+
.applyConnectionString(ConnectionString("<connection string>"))
10+
.applyToSslSettings { builder ->
11+
builder.enabled(true)
12+
builder.invalidHostNameAllowed(true)
13+
}
14+
.build()
15+
val mongoClient = MongoClient.create(settings);
16+
17+
18+
.. tab:: Connection String
19+
:tabid: connectionstring
20+
21+
.. code-block:: kotlin
22+
23+
val uri = "mongodb://<username>:<password>@<hostname>:<port>/?"tls=true&tlsAllowInvalidHostnames=true"
24+
val mongoClient = MongoClient.create(uri)

source/includes/connect/tls-tabs.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. tabs::
2+
3+
.. tab:: MongoClient
4+
:tabid: mongoclient
5+
6+
.. code-block:: kotlin
7+
8+
val settings = MongoClientSettings.builder()
9+
.applyConnectionString(ConnectionString("<connection string>"))
10+
.applyToSslSettings { builder ->
11+
builder.enabled(true)
12+
}
13+
.build()
14+
15+
val mongoClient = MongoClient.create(settings)
16+
17+
18+
.. tab:: Connection String
19+
:tabid: connectionstring
20+
21+
.. code-block:: kotlin
22+
23+
val uri = "mongodb+srv://<user>:<password>@<cluster-url>?tls=true"
24+
val mongoClient = MongoClient.create(uri)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. tabs::
2+
3+
.. tab:: MongoClient
4+
:tabid: mongoclient
5+
6+
.. code-block:: kotlin
7+
8+
val zlib = MongoCompressor.createZlibCompressor()
9+
10+
val settings = MongoClientSettings.builder()
11+
.applyConnectionString(ConnectionString(uri))
12+
.compressorList(listOf(zlib.withProperty(MongoCompressor.LEVEL, <level>)))
13+
.build()
14+
15+
val mongoClient = MongoClient.create(settings)
16+
17+
.. tab:: Connection String
18+
:tabid: connectionstring
19+
20+
.. code-block:: kotlin
21+
22+
val uri = "mongodb://<username>:<password>@<hostname>:<port>/?" +
23+
"compressors=zlib" +
24+
"zlibCompressionLevel=<zlib compression level>"
25+
26+
val mongoClient = MongoClient.create(uri)

source/includes/indexes/indexes.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.client.model.Indexes
4+
import com.mongodb.client.model.Filters
5+
import com.mongodb.client.model.Sorts
6+
import com.mongodb.kotlin.client.MongoClient
7+
import org.bson.codecs.pojo.annotations.BsonId
8+
import org.bson.types.ObjectId
9+
10+
// start-movie-class
11+
data class Movie(
12+
@BsonId
13+
val id: ObjectId,
14+
val title: String? = "",
15+
val type: String? = "",
16+
val genres: List<String>? = null,
17+
val cast: List<String>? = null,
18+
val plot: String? = "",
19+
)
20+
// end-movie-class
21+
22+
fun main() {
23+
val uri = "<connection string URI>"
24+
25+
val settings = MongoClientSettings.builder()
26+
.applyConnectionString(ConnectionString(uri))
27+
.retryWrites(true)
28+
.build()
29+
30+
val mongoClient = MongoClient.create(settings)
31+
val database = mongoClient.getDatabase("sample_mflix")
32+
val collection = database.getCollection<Movie>("movies")
33+
34+
// start-remove-index
35+
collection.dropIndex("_title_")
36+
// end-remove-index
37+
38+
// start-remove-all-indexes
39+
collection.dropIndexes()
40+
// end-remove-all-indexes
41+
42+
// start-index-single
43+
collection.createIndex(Indexes.ascending(Movie::title.name))
44+
// end-index-single
45+
46+
// start-index-single-query
47+
val filter = Filters.eq(Movie::title.name, "Batman")
48+
val sort = Sorts.ascending(Movie::title.name)
49+
val results = collection.find(filter).sort(sort)
50+
51+
results.forEach { result ->
52+
println(result)
53+
}
54+
// end-index-single-query
55+
56+
// start-index-compound
57+
collection.createIndex(Indexes.ascending(Movie::type.name, Movie::genres.name))
58+
// end-index-compound
59+
60+
// start-index-compound-query
61+
val filter = and(
62+
eq(Movie::type.name, "movie"),
63+
`in`(Movie::genres.name, "Drama")
64+
)
65+
val sort = Sorts.ascending(Movie::type.name, Movie::genres.name)
66+
val results = collection.find(filter).sort(sort)
67+
68+
results.forEach { result ->
69+
println(result)
70+
}
71+
// end-index-compound-query
72+
}

0 commit comments

Comments
 (0)