Skip to content

Commit 4b28c95

Browse files
authored
DOCSP-41132: Read Data from MongoDB (#3)
1 parent e213d40 commit 4b28c95

File tree

6 files changed

+249
-3
lines changed

6 files changed

+249
-3
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
==================================
2-
{+driver-long+} Documentation
3-
==================================
1+
=================================
2+
MongoDB Kotlin Sync Documentation
3+
=================================
44

55
This repository contains documentation for the {+driver-short+}.
66

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.kotlin.client.*
4+
import com.mongodb.client.model.Filters.*
5+
import org.bson.Document
6+
7+
fun main() {
8+
val uri = "<connection string URI>"
9+
10+
val settings = MongoClientSettings.builder()
11+
.applyConnectionString(ConnectionString(uri))
12+
.retryWrites(true)
13+
.build()
14+
15+
// Create a new client and connect to the server
16+
val mongoClient = MongoClient.create(settings)
17+
val database = mongoClient.getDatabase("<database name>")
18+
val collection = database.getCollection<Document>("<collection name>")
19+
20+
// start-find
21+
val filter = <filter>
22+
val results = collection.find(filter)
23+
results.forEach { result ->
24+
print(result)
25+
}
26+
// end-find
27+
28+
// start-count-all
29+
val count = collection.countDocuments()
30+
print(count)
31+
// end-count-all
32+
33+
// start-count-query
34+
val filter = <filter>
35+
val queryCount = collection.countDocuments(filter)
36+
print(queryCount)
37+
// end-count-query
38+
39+
// start-estimated-count
40+
val estimatedCount = collection.estimatedDocumentCount()
41+
print(estimatedCount)
42+
// end-estimated-count
43+
44+
// start-distinct
45+
val distinctResults = collection.distinct("<field name>")
46+
distinctResults.forEach { result ->
47+
print(result)
48+
}
49+
// end-distinct
50+
51+
// start-watch
52+
val changeStream = collection.watch()
53+
changeStream.forEach { changeEvent ->
54+
print(changeEvent)
55+
}
56+
// end-watch
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Sample Application
2+
~~~~~~~~~~~~~~~~~~
3+
4+
You can use the following sample application to test the code examples on this
5+
page. To use the sample application, perform the following steps:
6+
7+
1. Ensure you have the {+driver-short+} installed in your Maven or Gradle project.
8+
#. Copy the following code and paste it into a new ``.kt`` file.
9+
#. Copy a code example from this page and paste it on the specified lines in the file.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.kotlin.client.*
4+
import com.mongodb.client.model.Filters.*
5+
import org.bson.Document
6+
7+
fun main() {
8+
val uri = "<connection string URI>"
9+
10+
val settings = MongoClientSettings.builder()
11+
.applyConnectionString(ConnectionString(uri))
12+
.retryWrites(true)
13+
.build()
14+
15+
// Create a new client and connect to the server
16+
val mongoClient = MongoClient.create(settings)
17+
val database = mongoClient.getDatabase("<database name>")
18+
val collection = database.getCollection<Document>("<collection name>")
19+
20+
// Start example code here
21+
22+
// End example code here
23+
}

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:titlesonly:
77
:maxdepth: 1
88

9+
/read
910
/faq
1011
/connection-troubleshooting
1112
/issues-and-help

source/read.txt

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
.. _kotlin-sync-read:
2+
3+
======================
4+
Read Data from 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 read data from MongoDB.
19+
:keywords: usage examples, query, find, code example
20+
21+
.. .. toctree::
22+
.. :titlesonly:
23+
.. :maxdepth: 1
24+
25+
.. /read/specify-a-query
26+
.. /read/retrieve
27+
.. /read/project
28+
.. /read/specify-documents-to-return
29+
.. /read/count
30+
.. /read/distinct
31+
.. /read/cursors
32+
.. /read/change-streams
33+
34+
Overview
35+
--------
36+
37+
On this page, you can see copyable code examples that show common
38+
methods you can use to retrieve documents by using the {+driver-short+}.
39+
40+
.. tip::
41+
42+
To learn more about any of the methods shown on this page, see the link
43+
to a relevant guide provided in each section.
44+
45+
To use an example from this page, copy the code example into the
46+
:ref:`sample application <kotlin-sync-read-sample>` or your own application.
47+
Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with
48+
the relevant values for your MongoDB deployment.
49+
50+
.. _kotlin-sync-read-sample:
51+
52+
.. include:: /includes/usage-examples/sample-app-intro.rst
53+
54+
.. literalinclude:: /includes/usage-examples/sample-app.kt
55+
:language: kotlin
56+
:copyable:
57+
:linenos:
58+
:emphasize-lines: 20-22
59+
60+
.. .. tip::
61+
62+
.. For instructions about how to install the {+driver-short+}, see :ref:`<kotlin-sync-quick-start>`.
63+
64+
Find Documents
65+
--------------
66+
67+
The following example retrieves a list of documents that match the criteria specified by the
68+
given filter:
69+
70+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
71+
:start-after: start-find
72+
:end-before: end-find
73+
:language: kotlin
74+
:copyable:
75+
:dedent:
76+
77+
.. TODO: To learn more about the ``find()`` method, see the :ref:`kotlin-sync-retrieve-find` guide.
78+
79+
Count Documents in a Collection
80+
-------------------------------
81+
82+
The following example returns the number of documents in the specified collection:
83+
84+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
85+
:start-after: start-count-all
86+
:end-before: end-count-all
87+
:language: kotlin
88+
:copyable:
89+
:dedent:
90+
91+
.. TODO: To learn more about the ``count_documents()`` method, see the
92+
.. :ref:`kotlin-sync-accurate-count` guide.
93+
94+
Count Documents Returned from a Query
95+
-------------------------------------
96+
97+
The following example returns the number of documents that match the criteria specified by
98+
the given filter:
99+
100+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
101+
:start-after: start-count-query
102+
:end-before: end-count-query
103+
:language: kotlin
104+
:copyable:
105+
:dedent:
106+
107+
.. TODO: To learn more about the ``countDocuments()`` method, see the
108+
.. :ref:`kotlin-sync-accurate-count` guide.
109+
110+
Estimated Document Count
111+
------------------------
112+
113+
The following example returns an approximate number of documents in the specified
114+
collection based on collection metadata:
115+
116+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
117+
:start-after: start-estimated-count
118+
:end-before: end-estimated-count
119+
:language: kotlin
120+
:copyable:
121+
:dedent:
122+
123+
.. TODO: To learn more about the ``estimatedDocumentCount()`` method, see the
124+
.. :ref:`kotlin-sync-estimated-count` guide.
125+
126+
Retrieve Distinct Values
127+
------------------------
128+
129+
The following example returns all distinct values of the specified field name in a given
130+
collection:
131+
132+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
133+
:start-after: start-distinct
134+
:end-before: end-distinct
135+
:language: kotlin
136+
:copyable:
137+
:dedent:
138+
139+
.. TODO: To learn more about the ``distinct()`` method, see the
140+
.. :ref:`kotlin-sync-distinct` guide.
141+
142+
Monitor Data Changes
143+
--------------------
144+
145+
The following example creates a change stream for a given collection and prints out
146+
subsequent change events in that collection:
147+
148+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt
149+
:start-after: start-watch
150+
:end-before: end-watch
151+
:language: kotlin
152+
:copyable:
153+
:dedent:
154+
155+
.. TODO: To learn more about the ``watch()`` method, see the
156+
.. :ref:`kotlin-sync-change-streams` guide.

0 commit comments

Comments
 (0)