From 28eae0d7f58269ba5c28929d50f3a1cc0b91bf91 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 25 Jul 2024 14:42:53 -0400 Subject: [PATCH 1/3] DOCSP-41455: Standardize Count Documents page --- source/includes/read/count.kt | 52 ++++++++++++++++++++++++++ source/read/count.txt | 69 +++++++++++++++++++++-------------- 2 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 source/includes/read/count.kt diff --git a/source/includes/read/count.kt b/source/includes/read/count.kt new file mode 100644 index 0000000..b4046da --- /dev/null +++ b/source/includes/read/count.kt @@ -0,0 +1,52 @@ +package org.example +import com.mongodb.ConnectionString +import com.mongodb.MongoClientSettings +import com.mongodb.client.model.* +import com.mongodb.client.model.Filters.* +import com.mongodb.kotlin.client.MongoClient +import org.bson.codecs.pojo.annotations.BsonId +import org.bson.types.ObjectId + +// start-data-class +data class Movie( + @BsonId + val id: ObjectId, + val title: String +) +// end-data-class + +fun main() { + val uri = "mongodb+srv://michael:Scrubs1996@testcluster.kmosy7d.mongodb.net/?retryWrites=true&w=majority" + + val settings = MongoClientSettings.builder() + .applyConnectionString(ConnectionString(uri)) + .retryWrites(true) + .build() + + val mongoClient = MongoClient.create(settings) + val database = mongoClient.getDatabase("sample_mflix") + val collection = database.getCollection("movies") + + // start-count-all + println(collection.countDocuments()) + // end-count-all + + // start-count-query + println(collection.countDocuments(eq("year", "1930"))) + // end-count-query + + // start-count-options + val options = CountOptions().comment("Retrieving count") + collection.countDocuments(Filters.empty(), options) + // end-count-options + + // start-estimated-count + print(collection.estimatedDocumentCount()) + // end-estimated-count + + // start-estimated-count-options + val options = EstimatedDocumentCountOptions().comment("Retrieving count") + collection.estimatedDocumentCount(options) + // end-estimated-count-options +} + diff --git a/source/read/count.txt b/source/read/count.txt index e0ffe21..6d6410a 100644 --- a/source/read/count.txt +++ b/source/read/count.txt @@ -31,6 +31,14 @@ database from the :atlas:`Atlas sample datasets `. To learn how to free MongoDB Atlas cluster and load the sample datasets, see the :atlas:`Get Started with Atlas ` guide. +The following {+language+} data class models the documents in this collection: + +.. literalinclude:: /includes/read/project.kt + :start-after: start-data-class + :end-before: end-data-class + :language: kotlin + :copyable: + .. _kotlin-sync-accurate-count: Retrieve an Accurate Count @@ -40,7 +48,7 @@ Use the ``countDocuments()`` method to count the number of documents that are in collection. To count the number of documents that match specified search critera, pass a query filter to the ``countDocuments()`` method. -.. TODO: To learn more about specifying a query, see :ref:`kotlin-sync-specify-query`. +To learn more about specifying a query, see :ref:`kotlin-sync-specify-query`. Count All Documents ~~~~~~~~~~~~~~~~~~~ @@ -50,10 +58,11 @@ with no arguments, as shown in the following example: .. io-code-block:: - .. input:: + .. input:: /includes/read/count.kt + :start-after: start-count-all + :end-before: end-count-all :language: kotlin - - print(collection.countDocuments()) + :dedent: .. output:: :visible: false @@ -69,10 +78,11 @@ in the ``movies`` collection that have a ``year`` field value equal to ``1930``: .. io-code-block:: - .. input:: - :language: kotlin - - print(collection.countDocuments(eq("year", "1930"))) + .. input:: /includes/read/count.kt + :start-after: start-count-query + :end-before: end-count-query + :language: kotlin + :dedent: .. output:: :visible: false @@ -99,30 +109,33 @@ The following table describes the options you can set to customize ``countDocume - Description * - ``comment`` - - | A comment to attach to the operation. + - | Specifies a comment to attach to the operation. * - ``skip`` - - | The number of documents to skip before returning results. + - | Sets the number of documents to skip before returning results. * - ``limit`` - - | The maximum number of documents to count. Must be a positive integer. + - | Sets the maximum number of documents to count. Must be a positive integer. * - ``maxTime`` - - | The maximum amount of time to allow the operation to run, in milliseconds. + - | Sets the maximum amount of time to allow the operation to run, in milliseconds. * - ``collation`` - - | The collation to use for the operation. + - | Specifies the kind of language collation to use when sorting + results. For more information, see :manual:`Collation ` + in the {+mdb-server+} manual. * - ``hint`` - - | Gets or sets the index to scan for documents. + - | Sets the index to scan for documents. The following example uses a ``CountOptions`` object to add a comment to the ``countDocuments()`` operation: -.. code-block:: kotlin - - val options = CountOptions().comment("Retrieving count") - collection.countDocuments(options) +.. literalinclude:: /includes/read/count.kt + :start-after: start-count-options + :end-before: end-count-options + :language: kotlin + :dedent: .. _kotlin-sync-estimated-count: @@ -137,10 +150,11 @@ The following example prints the estimated number of documents in a collection: .. io-code-block:: - .. input:: + .. input:: /includes/read/count.kt + :start-after: start-estimated-count + :end-before: end-estimated-count :language: kotlin - - print(collection.estimatedDocumentCount()) + :dedent: .. output:: :visible: false @@ -167,18 +181,19 @@ The following table describes the options you can set to customize ``estimatedDo - Description * - ``comment`` - - | A comment to attach to the operation. + - | Specifies a comment to attach to the operation. * - ``maxTime`` - - | The maximum amount of time to allow the operation to run, in milliseconds. + - | Specifies the maximum amount of time to allow the operation to run, in milliseconds. The following example uses an ``EstimatedDocumentCountOptions`` object to add a comment to the ``estimatedDocumentCount()`` operation: -.. code-block:: kotlin - - val options = EstimatedDocumentCountOptions().comment("Retrieving count") - collection.estimatedDocumentCount(options) +.. literalinclude:: /includes/read/count.kt + :start-after: start-estimated-count-options + :end-before: end-estimated-count-options + :language: kotlin + :dedent: API Documentation ----------------- From e2586e437f0400769cb61bb207f5b879de4a1cdf Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 25 Jul 2024 14:45:11 -0400 Subject: [PATCH 2/3] Fix --- source/read/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/read/count.txt b/source/read/count.txt index 6d6410a..d0952d4 100644 --- a/source/read/count.txt +++ b/source/read/count.txt @@ -33,7 +33,7 @@ free MongoDB Atlas cluster and load the sample datasets, see the The following {+language+} data class models the documents in this collection: -.. literalinclude:: /includes/read/project.kt +.. literalinclude:: /includes/read/count.kt :start-after: start-data-class :end-before: end-data-class :language: kotlin From 8fe1a63e132f088fc770b28875cb5d81a260cad8 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 25 Jul 2024 14:46:12 -0400 Subject: [PATCH 3/3] Fix --- source/includes/read/count.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/source/includes/read/count.kt b/source/includes/read/count.kt index b4046da..c01957e 100644 --- a/source/includes/read/count.kt +++ b/source/includes/read/count.kt @@ -1,4 +1,3 @@ -package org.example import com.mongodb.ConnectionString import com.mongodb.MongoClientSettings import com.mongodb.client.model.*