Skip to content

Commit 073f6a6

Browse files
committed
DOCSP-41455: Standardize Count Documents page (#22)
(cherry picked from commit 6bd92e0)
1 parent 4d2bd75 commit 073f6a6

File tree

2 files changed

+93
-27
lines changed

2 files changed

+93
-27
lines changed

source/includes/read/count.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.client.model.*
4+
import com.mongodb.client.model.Filters.*
5+
import com.mongodb.kotlin.client.MongoClient
6+
import org.bson.codecs.pojo.annotations.BsonId
7+
import org.bson.types.ObjectId
8+
9+
// start-data-class
10+
data class Movie(
11+
@BsonId
12+
val id: ObjectId,
13+
val title: String
14+
)
15+
// end-data-class
16+
17+
fun main() {
18+
val uri = "mongodb+srv://michael:[email protected]/?retryWrites=true&w=majority"
19+
20+
val settings = MongoClientSettings.builder()
21+
.applyConnectionString(ConnectionString(uri))
22+
.retryWrites(true)
23+
.build()
24+
25+
val mongoClient = MongoClient.create(settings)
26+
val database = mongoClient.getDatabase("sample_mflix")
27+
val collection = database.getCollection<Movie>("movies")
28+
29+
// start-count-all
30+
println(collection.countDocuments())
31+
// end-count-all
32+
33+
// start-count-query
34+
println(collection.countDocuments(eq("year", "1930")))
35+
// end-count-query
36+
37+
// start-count-options
38+
val options = CountOptions().comment("Retrieving count")
39+
collection.countDocuments(Filters.empty(), options)
40+
// end-count-options
41+
42+
// start-estimated-count
43+
print(collection.estimatedDocumentCount())
44+
// end-estimated-count
45+
46+
// start-estimated-count-options
47+
val options = EstimatedDocumentCountOptions().comment("Retrieving count")
48+
collection.estimatedDocumentCount(options)
49+
// end-estimated-count-options
50+
}
51+

source/read/count.txt

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to
3131
free MongoDB Atlas cluster and load the sample datasets, see the
3232
:atlas:`Get Started with Atlas </getting-started>` guide.
3333

34+
The following {+language+} data class models the documents in this collection:
35+
36+
.. literalinclude:: /includes/read/count.kt
37+
:start-after: start-data-class
38+
:end-before: end-data-class
39+
:language: kotlin
40+
:copyable:
41+
3442
.. _kotlin-sync-accurate-count:
3543

3644
Retrieve an Accurate Count
@@ -40,7 +48,7 @@ Use the ``countDocuments()`` method to count the number of documents that are in
4048
collection. To count the number of documents that match specified search
4149
critera, pass a query filter to the ``countDocuments()`` method.
4250

43-
.. TODO: To learn more about specifying a query, see :ref:`kotlin-sync-specify-query`.
51+
To learn more about specifying a query, see :ref:`kotlin-sync-specify-query`.
4452

4553
Count All Documents
4654
~~~~~~~~~~~~~~~~~~~
@@ -50,10 +58,11 @@ with no arguments, as shown in the following example:
5058

5159
.. io-code-block::
5260

53-
.. input::
61+
.. input:: /includes/read/count.kt
62+
:start-after: start-count-all
63+
:end-before: end-count-all
5464
:language: kotlin
55-
56-
print(collection.countDocuments())
65+
:dedent:
5766

5867
.. output::
5968
:visible: false
@@ -69,10 +78,11 @@ in the ``movies`` collection that have a ``year`` field value equal to ``1930``:
6978

7079
.. io-code-block::
7180

72-
.. input::
73-
:language: kotlin
74-
75-
print(collection.countDocuments(eq("year", "1930")))
81+
.. input:: /includes/read/count.kt
82+
:start-after: start-count-query
83+
:end-before: end-count-query
84+
:language: kotlin
85+
:dedent:
7686

7787
.. output::
7888
:visible: false
@@ -99,30 +109,33 @@ The following table describes the options you can set to customize ``countDocume
99109
- Description
100110

101111
* - ``comment``
102-
- | A comment to attach to the operation.
112+
- | Specifies a comment to attach to the operation.
103113

104114
* - ``skip``
105-
- | The number of documents to skip before returning results.
115+
- | Sets the number of documents to skip before returning results.
106116

107117
* - ``limit``
108-
- | The maximum number of documents to count. Must be a positive integer.
118+
- | Sets the maximum number of documents to count. Must be a positive integer.
109119

110120
* - ``maxTime``
111-
- | The maximum amount of time to allow the operation to run, in milliseconds.
121+
- | Sets the maximum amount of time to allow the operation to run, in milliseconds.
112122

113123
* - ``collation``
114-
- | The collation to use for the operation.
124+
- | Specifies the kind of language collation to use when sorting
125+
results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>`
126+
in the {+mdb-server+} manual.
115127

116128
* - ``hint``
117-
- | Gets or sets the index to scan for documents.
129+
- | Sets the index to scan for documents.
118130

119131
The following example uses a ``CountOptions`` object to add a comment to the
120132
``countDocuments()`` operation:
121133

122-
.. code-block:: kotlin
123-
124-
val options = CountOptions().comment("Retrieving count")
125-
collection.countDocuments(options)
134+
.. literalinclude:: /includes/read/count.kt
135+
:start-after: start-count-options
136+
:end-before: end-count-options
137+
:language: kotlin
138+
:dedent:
126139

127140
.. _kotlin-sync-estimated-count:
128141

@@ -137,10 +150,11 @@ The following example prints the estimated number of documents in a collection:
137150

138151
.. io-code-block::
139152

140-
.. input::
153+
.. input:: /includes/read/count.kt
154+
:start-after: start-estimated-count
155+
:end-before: end-estimated-count
141156
:language: kotlin
142-
143-
print(collection.estimatedDocumentCount())
157+
:dedent:
144158

145159
.. output::
146160
:visible: false
@@ -167,18 +181,19 @@ The following table describes the options you can set to customize ``estimatedDo
167181
- Description
168182

169183
* - ``comment``
170-
- | A comment to attach to the operation.
184+
- | Specifies a comment to attach to the operation.
171185

172186
* - ``maxTime``
173-
- | The maximum amount of time to allow the operation to run, in milliseconds.
187+
- | Specifies the maximum amount of time to allow the operation to run, in milliseconds.
174188

175189
The following example uses an ``EstimatedDocumentCountOptions`` object to add a comment to
176190
the ``estimatedDocumentCount()`` operation:
177191

178-
.. code-block:: kotlin
179-
180-
val options = EstimatedDocumentCountOptions().comment("Retrieving count")
181-
collection.estimatedDocumentCount(options)
192+
.. literalinclude:: /includes/read/count.kt
193+
:start-after: start-estimated-count-options
194+
:end-before: end-estimated-count-options
195+
:language: kotlin
196+
:dedent:
182197

183198
API Documentation
184199
-----------------

0 commit comments

Comments
 (0)