From 23fab2d2d31816282fdd17e9f89856a51c288064 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 13:50:05 -0400 Subject: [PATCH 01/28] DOCSP-28793: Count Fundementals --- .../crud/read-operations/count.txt | 262 ++++++++++++++++++ .../crud/read-operations/specify-query.txt | 2 - source/includes/code-examples/FinalGrades.cs | 7 + 3 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 source/fundamentals/crud/read-operations/count.txt create mode 100644 source/includes/code-examples/FinalGrades.cs diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt new file mode 100644 index 00000000..319217f6 --- /dev/null +++ b/source/fundamentals/crud/read-operations/count.txt @@ -0,0 +1,262 @@ +.. _csharp-count-documents: + +=============== +Count Documents +=============== + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +Overview +-------- + +In this guide, you can learn how to get an :ref:`accurate +` and :ref:`estimated ` count of +the number of documents in your collection. + +Sample Data +~~~~~~~~~~~ + +The examples in this guide use the following documents in a collection called +``grades``: + +.. code-block:: json + + { "_id": 1, "name": "Spongebob", "finalGrade": 87.5} + { "_id": 2, "name": "Patrick", "finalGrade": 12.3} + { "_id": 3, "name": "Squidward", "finalGrade": 99.0} + { "_id": 4, "name": "Sandy", "finalGrade": 85.5} + { "_id": 5, "name": "Plankton", "finalGrade": 72.3} + { "_id": 6, "name": "Karen", "finalGrade": 88.8} + +The following ``FinalGrades`` class models the documents in this +collection. + +.. literalinclude:: /includes/fundamentals/code-examples/specify-query/FinalGrades.cs + :language: csharp + :copyable: + :dedent: + +.. note:: + + The documents in the ``grades`` collection use the camel-case naming + convention. The examples in this guide use a ``ConventionPack`` + to deserialize the fields in the collection into Pascal case and map them to + the properties in the ``FinalGrades`` class. + + To learn more about custom serialization, see + :ref:`csharp-custom-serialization`. + +.. _csharp-accurate-count: + +Accurate Count +-------------- + +To count the number of documents that match your :ref`query filter`, use the +``CountDocuments()`` method. If you pass an empty query filter, this method +returns the total number of documents in the collection. + +Example +``````` + +The following example counts the number of documents where the +``finalGrade`` is less than ``80``: + +.. io-code-block:: + :copyable: true + + .. input:: + :language: csharp + + var filter = Builders.Filter.Lt("finalGrade", 80); + var count = coll.CountDocuments(filter); + + Console.WriteLine("Number of documents with a final grade less + than 80: " + count); + + .. output:: + :language: none + :visible: false + + Number of documents with a final grade less than 80: 2 + +Modify Behavior +~~~~~~~~~~~~~~~ + +You can modify the behavior of ``CountDocuments()`` by passing in a +``CountOptions`` type. If you don't specify any options, the driver uses +its default values. + +The ``CountOptions`` type allows you to configure options with the +following properties: + +.. list-table:: + :widths: 30 70 + :header-rows: 1 + + * - Property + - Description + + * - ``Collation`` + - | The type of language collation to use when sorting results. + | Default: ``nil`` + + * - ``Hint`` + - | The index to use to scan for documents to count. + | Default: ``nil`` + + * - ``Limit`` + - | The maximum number of documents to count. + | Default: ``0`` + + * - ``MaxTime`` + - | The maximum amount of time that the query can run on the server. + | Default: ``nil`` + + * - ``Skip`` + - | The number of documents to skip before counting. + | Default: ``0`` + +.. tip:: + + When you use ``CountDocuments()`` to return the total number of documents in a + collection, MongoDB performs a collection scan. You can avoid a collection scan and + improve the performance of this method by using a hint to take advantage of the built-in index on + the ``_id`` field. Use this technique only when calling ``CountDocuments()`` + with an empty query parameter. + + .. code-block:: csharp + :emphasize-lines: 1, 3 + + CountOptions opts = new CountOptions(){Hint = "_id_"}; + + var counter = collection.CountDocuments(filter, opts); + +.. _csharp-estimated-count: + +Estimated Count +--------------- + +To estimate the number of documents in your collection, use the +``EstimatedDocumentCount()`` method. + +.. note:: + + The ``EstimatedDocumentCount()`` method is quicker than the + ``CountDocuments()`` method because it uses the collection's + metadata rather than scanning the entire collection. + +Modify Behavior +~~~~~~~~~~~~~~~ + +You can modify the behavior of ``EstimatedDocumentCount()`` by passing +in an ``EstimatedDocumentCountOptions`` type. If you don't specify any +options, the driver uses its default values. + +The ``EstimatedDocumentCountOptions`` type allows you to configure +options with the following property: + +.. list-table:: + :widths: 30 70 + :header-rows: 1 + + * - Property + - Description + + * - ``MaxTime`` + - | The maximum amount of time that the query can run on the server. + | Default: ``nil`` + +Example +``````` + +The following example estimates the number of documents in the +``ratings`` collection: + +.. io-code-block:: + :copyable: true + + .. input:: + :language: go + + count, err := coll.EstimatedDocumentCount(context.TODO()) + if err != nil { + panic(err) + } + fmt.Printf("Estimated number of documents in the ratings collection: %d\n", count) + + .. output:: + :language: none + :visible: false + + Estimated number of documents in the ratings collection: 9 + +.. _csharp-count-aggregation: + +Aggregation +----------- + +You can also include the :manual:`$count ` +stage to count the number of documents in an aggregation pipeline. + +Example +~~~~~~~ + +The following example performs the following actions: + +- Counts the number of documents where the ``rating`` is greater than ``5`` +- Assigns the count to the ``counted_documents`` field + +.. io-code-block:: + :copyable: true + + .. input:: + :language: go + + matchStage := bson.D{{"$match", bson.D{{"rating", bson.D{{"$gt", 5}}}}}} + countStage := bson.D{{"$count", "counted_documents"}} + + cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, countStage}) + if err != nil { + panic(err) + } + + var results []bson.D + if err = cursor.All(context.TODO(), &results); err != nil { + panic(err) + } + for _, result := range results { + fmt.Println(result) + } + + .. output:: + :language: none + :visible: false + + [{counted_documents 5}] + +Additional Information +---------------------- + +To learn more about the operations mentioned, see the following +guides: + +- :ref:`golang-query-document` +- :ref:`golang-skip` +- :ref:`golang-limit` +- :ref:`golang-aggregation` +- :ref:`golang-collations` + +API Documentation +~~~~~~~~~~~~~~~~~ + +To learn more about any of the methods or types discussed in this +guide, see the following API Documentation: + +- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__ +- `CountOptions <{+api+}/mongo/options#CountOptions>`__ +- `EstimatedDocumentCount() <{+api+}/mongo#Collection.EstimatedDocumentCount>`__ +- `EstimatedDocumentCountOptions <{+api+}/mongo/options#EstimatedDocumentCountOptions>`__ diff --git a/source/fundamentals/crud/read-operations/specify-query.txt b/source/fundamentals/crud/read-operations/specify-query.txt index 010886f4..22bfa38b 100644 --- a/source/fundamentals/crud/read-operations/specify-query.txt +++ b/source/fundamentals/crud/read-operations/specify-query.txt @@ -4,8 +4,6 @@ Specify a Query =============== -.. default-domain:: mongodb - .. contents:: On this page :local: :backlinks: none diff --git a/source/includes/code-examples/FinalGrades.cs b/source/includes/code-examples/FinalGrades.cs new file mode 100644 index 00000000..752d233b --- /dev/null +++ b/source/includes/code-examples/FinalGrades.cs @@ -0,0 +1,7 @@ +public class FinalGrades { + public ObjectId Id { get; set; } + + public string StudentName { get; set; } + + public double FinalGrade { get; set; } +} \ No newline at end of file From b0b223f8fb9728aded46ceca893f3eb6b002d6da Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:03:54 -0400 Subject: [PATCH 02/28] test --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 319217f6..fc5cd597 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -45,7 +45,7 @@ collection. The documents in the ``grades`` collection use the camel-case naming convention. The examples in this guide use a ``ConventionPack`` to deserialize the fields in the collection into Pascal case and map them to - the properties in the ``FinalGrades`` class. + the properties in the ``FinalGrades`` class. To learn more about custom serialization, see :ref:`csharp-custom-serialization`. From 1e45996c5901b53780698f53cd80f02c3c658a1f Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:05:27 -0400 Subject: [PATCH 03/28] test --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index fc5cd597..80e2022e 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -48,7 +48,7 @@ collection. the properties in the ``FinalGrades`` class. To learn more about custom serialization, see - :ref:`csharp-custom-serialization`. + :ref:`csharp-custom-serialization`. .. _csharp-accurate-count: From 1d827d18da428090e09fe60e05492e773b0856cd Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:07:27 -0400 Subject: [PATCH 04/28] test --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 80e2022e..fc5cd597 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -48,7 +48,7 @@ collection. the properties in the ``FinalGrades`` class. To learn more about custom serialization, see - :ref:`csharp-custom-serialization`. + :ref:`csharp-custom-serialization`. .. _csharp-accurate-count: From 0afcb50351ecbbf36072c58d144cd7787430e768 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:08:38 -0400 Subject: [PATCH 05/28] test --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index fc5cd597..80e2022e 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -48,7 +48,7 @@ collection. the properties in the ``FinalGrades`` class. To learn more about custom serialization, see - :ref:`csharp-custom-serialization`. + :ref:`csharp-custom-serialization`. .. _csharp-accurate-count: From 0478f453e17aa4cb54ee7150b146fd2c1eb12ce0 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:16:47 -0400 Subject: [PATCH 06/28] test --- source/fundamentals/crud/read-operations.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/fundamentals/crud/read-operations.txt b/source/fundamentals/crud/read-operations.txt index bc6d1fc9..9b16e526 100644 --- a/source/fundamentals/crud/read-operations.txt +++ b/source/fundamentals/crud/read-operations.txt @@ -9,6 +9,8 @@ Read Operations /fundamentals/crud/read-operations/retrieve /fundamentals/crud/read-operations/specify-query + /fundamentals/crud/read-operations/count - :ref:`csharp-retrieve` - :ref:`csharp-specify-query` +- :ref:`csharp-count-documents` From 9e191ad2cf69ce2f8bcbff6cd058c0fe2083e4d6 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:49:21 -0400 Subject: [PATCH 07/28] up through estimated --- .../crud/read-operations/count.txt | 26 ++++++++++--------- .../{ => count-documents}/FinalGrades.cs | 6 ++--- 2 files changed, 17 insertions(+), 15 deletions(-) rename source/includes/code-examples/{ => count-documents}/FinalGrades.cs (77%) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 80e2022e..80646f48 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,9 +35,10 @@ The examples in this guide use the following documents in a collection called The following ``FinalGrades`` class models the documents in this collection. -.. literalinclude:: /includes/fundamentals/code-examples/specify-query/FinalGrades.cs +.. literalinclude:: /includes/fundamentals/code-examples/count-documents/FinalGrades.cs + :start-after: start-grades-struct + :end-before: end-grades-struct :language: csharp - :copyable: :dedent: .. note:: @@ -55,7 +56,7 @@ collection. Accurate Count -------------- -To count the number of documents that match your :ref`query filter`, use the +To count the number of documents that match your :ref:`query filter `, use the ``CountDocuments()`` method. If you pass an empty query filter, this method returns the total number of documents in the collection. @@ -131,8 +132,9 @@ following properties: .. code-block:: csharp :emphasize-lines: 1, 3 + var filter = Builders.Filter.Empty; + CountOptions opts = new CountOptions(){Hint = "_id_"}; - var counter = collection.CountDocuments(filter, opts); .. _csharp-estimated-count: @@ -140,7 +142,8 @@ following properties: Estimated Count --------------- -To estimate the number of documents in your collection, use the +To estimate the number of documents in your collection without the use +of a filter, use the ``EstimatedDocumentCount()`` method. .. note:: @@ -180,19 +183,18 @@ The following example estimates the number of documents in the :copyable: true .. input:: - :language: go + :language: csharp - count, err := coll.EstimatedDocumentCount(context.TODO()) - if err != nil { - panic(err) - } - fmt.Printf("Estimated number of documents in the ratings collection: %d\n", count) + var count = collection.EstimatedDocumentCount(); + String output = String.Format("Estimated number of documents in + the grades collection: " + count); + Console.WriteLine(output); .. output:: :language: none :visible: false - Estimated number of documents in the ratings collection: 9 + Estimated number of documents in the grades collection: 6 .. _csharp-count-aggregation: diff --git a/source/includes/code-examples/FinalGrades.cs b/source/includes/code-examples/count-documents/FinalGrades.cs similarity index 77% rename from source/includes/code-examples/FinalGrades.cs rename to source/includes/code-examples/count-documents/FinalGrades.cs index 752d233b..2374c02b 100644 --- a/source/includes/code-examples/FinalGrades.cs +++ b/source/includes/code-examples/count-documents/FinalGrades.cs @@ -1,7 +1,7 @@ +//start-grades-struct public class FinalGrades { public ObjectId Id { get; set; } - public string StudentName { get; set; } - public double FinalGrade { get; set; } -} \ No newline at end of file +} +//end-grades-struct \ No newline at end of file From b342bf5b049047f83eb6f11271b0bada0ec13fcc Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 14:56:18 -0400 Subject: [PATCH 08/28] up through estimated --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 80646f48..0c1e4c61 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``FinalGrades`` class models the documents in this collection. -.. literalinclude:: /includes/fundamentals/code-examples/count-documents/FinalGrades.cs +.. literalinclude:: /includes/code-examples/count-documents/FinalGrades.cs :start-after: start-grades-struct :end-before: end-grades-struct :language: csharp From 6b03c37ed5334b054fa8b70d876fc0e371ad45e3 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 16:57:55 -0400 Subject: [PATCH 09/28] aggregate --- .../crud/read-operations/count.txt | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 0c1e4c61..e22dd095 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``FinalGrades`` class models the documents in this collection. -.. literalinclude:: /includes/code-examples/count-documents/FinalGrades.cs +.. literalinclude:: /includes/fundamentals/code-examples/count-documents/FinalGrades.cs :start-after: start-grades-struct :end-before: end-grades-struct :language: csharp @@ -209,36 +209,27 @@ Example The following example performs the following actions: -- Counts the number of documents where the ``rating`` is greater than ``5`` -- Assigns the count to the ``counted_documents`` field +- Counts the number of documents where the ``final grade`` is less than ``80`` +- Assigns the count to the ``count`` field .. io-code-block:: :copyable: true .. input:: - :language: go - - matchStage := bson.D{{"$match", bson.D{{"rating", bson.D{{"$gt", 5}}}}}} - countStage := bson.D{{"$count", "counted_documents"}} - - cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, countStage}) - if err != nil { - panic(err) - } + :language: csharp - var results []bson.D - if err = cursor.All(context.TODO(), &results); err != nil { - panic(err) - } - for _, result := range results { - fmt.Println(result) - } + var matchStage = Builders.Filter.Gt(f => + f.finalGrade, 80); + var pipeline = new EmptyPipelineDefinition() + .Match(matchStage).Count(); + Console.WriteLine(pipeline); .. output:: :language: none :visible: false - [{counted_documents 5}] + [{ "$match" : { "finalGrade" : { "$gt" : 80.0 } } }, + { "$count" : "count" }] Additional Information ---------------------- From 7e5d5fe1adb26aad0934b01592313b3af4970165 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 17:19:59 -0400 Subject: [PATCH 10/28] final? --- source/fundamentals/crud/read-operations/count.txt | 12 ++++++------ .../fundamentals/data-formats/guid-serialization.txt | 2 -- source/fundamentals/data-formats/poco.txt | 2 -- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index e22dd095..9ac0c59e 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -32,7 +32,7 @@ The examples in this guide use the following documents in a collection called { "_id": 5, "name": "Plankton", "finalGrade": 72.3} { "_id": 6, "name": "Karen", "finalGrade": 88.8} -The following ``FinalGrades`` class models the documents in this +The following ``FinalGrades`` class, or :ref:`poco `, models the documents in this collection. .. literalinclude:: /includes/fundamentals/code-examples/count-documents/FinalGrades.cs @@ -237,11 +237,11 @@ Additional Information To learn more about the operations mentioned, see the following guides: -- :ref:`golang-query-document` -- :ref:`golang-skip` -- :ref:`golang-limit` -- :ref:`golang-aggregation` -- :ref:`golang-collations` +- :ref:`csharp-specify-query` +- :ref:`csharp-bson` +- :ref:`csharp-guids` +- :ref:`csharp-builders` +- :ref:`csharp-poco` API Documentation ~~~~~~~~~~~~~~~~~ diff --git a/source/fundamentals/data-formats/guid-serialization.txt b/source/fundamentals/data-formats/guid-serialization.txt index ef4607dc..5d8b6812 100644 --- a/source/fundamentals/data-formats/guid-serialization.txt +++ b/source/fundamentals/data-formats/guid-serialization.txt @@ -4,8 +4,6 @@ GUID Serialization ================== -.. default-domain:: mongodb - .. contents:: On this page :local: :backlinks: none diff --git a/source/fundamentals/data-formats/poco.txt b/source/fundamentals/data-formats/poco.txt index 38db86de..96bbe0dc 100644 --- a/source/fundamentals/data-formats/poco.txt +++ b/source/fundamentals/data-formats/poco.txt @@ -4,8 +4,6 @@ Work with POCOs =============== -.. default-domain:: mongodb - .. contents:: On this page :local: :backlinks: none From e8c5835a516f4761324febc23cdba68a08dc6c90 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 17:26:13 -0400 Subject: [PATCH 11/28] final? --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 9ac0c59e..f165089b 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``FinalGrades`` class, or :ref:`poco `, models the documents in this collection. -.. literalinclude:: /includes/fundamentals/code-examples/count-documents/FinalGrades.cs +.. literalinclude:: /source/includes/fundamentals/code-examples/count-documents/FinalGrades.cs :start-after: start-grades-struct :end-before: end-grades-struct :language: csharp From 511616e0937459126ad3694426279c23851589b6 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Tue, 20 Jun 2023 17:30:24 -0400 Subject: [PATCH 12/28] final? --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index f165089b..d57e97cf 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``FinalGrades`` class, or :ref:`poco `, models the documents in this collection. -.. literalinclude:: /source/includes/fundamentals/code-examples/count-documents/FinalGrades.cs +.. literalinclude:: /includes/code-examples/count-documents/FinalGrades.cs :start-after: start-grades-struct :end-before: end-grades-struct :language: csharp From e6a1418745440f451d3cebe47459a3e6923c7164 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 10:31:37 -0400 Subject: [PATCH 13/28] final --- source/fundamentals/crud/read-operations/count.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index d57e97cf..a99c60bb 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -249,7 +249,7 @@ API Documentation To learn more about any of the methods or types discussed in this guide, see the following API Documentation: -- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__ -- `CountOptions <{+api+}/mongo/options#CountOptions>`__ -- `EstimatedDocumentCount() <{+api+}/mongo#Collection.EstimatedDocumentCount>`__ -- `EstimatedDocumentCountOptions <{+api+}/mongo/options#EstimatedDocumentCountOptions>`__ +- `CountDocuments() <{+api+}/M_MongoDB_Driver_IMongoCollection_1_CountDocuments.htm>`__ +- `CountOptions <{+api+}/T_MongoDB_Driver_CountOptions.htm>`__ +- `EstimatedDocumentCount() <{+api+}/M_MongoDB_Driver_MongoCollectionBase_1_EstimatedDocumentCount.htm>`__ +- `EstimatedDocumentCountOptions <{+api+}/T_MongoDB_Driver_EstimatedDocumentCountOptions.htm>`__ From 1957fd52ad3d5a33b21e7c109a5ebce0261992cd Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 10:36:52 -0400 Subject: [PATCH 14/28] api fix --- source/fundamentals/crud/read-operations/count.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index a99c60bb..ea9b3275 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -249,7 +249,7 @@ API Documentation To learn more about any of the methods or types discussed in this guide, see the following API Documentation: -- `CountDocuments() <{+api+}/M_MongoDB_Driver_IMongoCollection_1_CountDocuments.htm>`__ -- `CountOptions <{+api+}/T_MongoDB_Driver_CountOptions.htm>`__ -- `EstimatedDocumentCount() <{+api+}/M_MongoDB_Driver_MongoCollectionBase_1_EstimatedDocumentCount.htm>`__ -- `EstimatedDocumentCountOptions <{+api+}/T_MongoDB_Driver_EstimatedDocumentCountOptions.htm>`__ +- `CountDocuments() <{+api-root+}/M_MongoDB_Driver_IMongoCollection_1_CountDocuments.htm>`__ +- `CountOptions <{+api-root+}/T_MongoDB_Driver_CountOptions.htm>`__ +- `EstimatedDocumentCount() <{+api-root+}/M_MongoDB_Driver_MongoCollectionBase_1_EstimatedDocumentCount.htm>`__ +- `EstimatedDocumentCountOptions <{+api-root+}/T_MongoDB_Driver_EstimatedDocumentCountOptions.htm>`__ From 90243345364374a30971c64af4e79f5aa2ce182b Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 10:58:50 -0400 Subject: [PATCH 15/28] final except error --- source/fundamentals/crud/read-operations/count.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index ea9b3275..927177d7 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -32,7 +32,8 @@ The examples in this guide use the following documents in a collection called { "_id": 5, "name": "Plankton", "finalGrade": 72.3} { "_id": 6, "name": "Karen", "finalGrade": 88.8} -The following ``FinalGrades`` class, or :ref:`poco `, models the documents in this +The following ``FinalGrades`` class is a Plain Old CLR/Class Object, or +:ref:`poco `, that models the documents in this collection. .. literalinclude:: /includes/code-examples/count-documents/FinalGrades.cs From 098ad48529014adb74912b1e42cbb81d3de76b46 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 11:18:27 -0400 Subject: [PATCH 16/28] final --- .../crud/read-operations/count.txt | 20 +++++++++---------- .../{FinalGrades.cs => Student.cs} | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) rename source/includes/code-examples/count-documents/{FinalGrades.cs => Student.cs} (63%) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 927177d7..fdc7e760 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -32,11 +32,11 @@ The examples in this guide use the following documents in a collection called { "_id": 5, "name": "Plankton", "finalGrade": 72.3} { "_id": 6, "name": "Karen", "finalGrade": 88.8} -The following ``FinalGrades`` class is a Plain Old CLR/Class Object, or +The following ``Student`` class is a Plain Old CLR/Class Object, or :ref:`poco `, that models the documents in this collection. -.. literalinclude:: /includes/code-examples/count-documents/FinalGrades.cs +.. literalinclude:: /includes/code-examples/count-documents/Student.cs :start-after: start-grades-struct :end-before: end-grades-struct :language: csharp @@ -47,7 +47,7 @@ collection. The documents in the ``grades`` collection use the camel-case naming convention. The examples in this guide use a ``ConventionPack`` to deserialize the fields in the collection into Pascal case and map them to - the properties in the ``FinalGrades`` class. + the properties in the ``Student`` class. To learn more about custom serialization, see :ref:`csharp-custom-serialization`. @@ -62,7 +62,7 @@ To count the number of documents that match your :ref:`query filter .Filter.Empty; + var filter = Builders.Filter.Empty; CountOptions opts = new CountOptions(){Hint = "_id_"}; var counter = collection.CountDocuments(filter, opts); @@ -178,7 +178,7 @@ Example ``````` The following example estimates the number of documents in the -``ratings`` collection: +``grades`` collection: .. io-code-block:: :copyable: true @@ -210,7 +210,7 @@ Example The following example performs the following actions: -- Counts the number of documents where the ``final grade`` is less than ``80`` +- Counts the number of documents where the ``finalGrade`` is greater than ``80`` - Assigns the count to the ``count`` field .. io-code-block:: @@ -219,9 +219,9 @@ The following example performs the following actions: .. input:: :language: csharp - var matchStage = Builders.Filter.Gt(f => - f.finalGrade, 80); - var pipeline = new EmptyPipelineDefinition() + var matchStage = Builders + .Filter.Gt(f => f.finalGrade, 80); + var pipeline = new EmptyPipelineDefinition() .Match(matchStage).Count(); Console.WriteLine(pipeline); diff --git a/source/includes/code-examples/count-documents/FinalGrades.cs b/source/includes/code-examples/count-documents/Student.cs similarity index 63% rename from source/includes/code-examples/count-documents/FinalGrades.cs rename to source/includes/code-examples/count-documents/Student.cs index 2374c02b..b2918a16 100644 --- a/source/includes/code-examples/count-documents/FinalGrades.cs +++ b/source/includes/code-examples/count-documents/Student.cs @@ -1,7 +1,7 @@ //start-grades-struct -public class FinalGrades { +public class Student { public ObjectId Id { get; set; } - public string StudentName { get; set; } + public string Name { get; set; } public double FinalGrade { get; set; } } //end-grades-struct \ No newline at end of file From a3af65f4ef6f18b1505833819c2af142b9ab1fe1 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 13:33:42 -0400 Subject: [PATCH 17/28] post review --- .../crud/read-operations/count.txt | 44 +++++++++---------- .../code-examples/count-documents/Student.cs | 16 +++++-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index fdc7e760..c97fd488 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -21,30 +21,29 @@ Sample Data ~~~~~~~~~~~ The examples in this guide use the following documents in a collection called -``grades``: +``students``: .. code-block:: json - { "_id": 1, "name": "Spongebob", "finalGrade": 87.5} - { "_id": 2, "name": "Patrick", "finalGrade": 12.3} - { "_id": 3, "name": "Squidward", "finalGrade": 99.0} - { "_id": 4, "name": "Sandy", "finalGrade": 85.5} - { "_id": 5, "name": "Plankton", "finalGrade": 72.3} - { "_id": 6, "name": "Karen", "finalGrade": 88.8} - -The following ``Student`` class is a Plain Old CLR/Class Object, or -:ref:`poco `, that models the documents in this + { "_id": 1, "name": "Jonathon Howard ", "finalGrade": 87.5 } + { "_id": 2, "name": "Keisha Freeman", "finalGrade": 12.3 } + { "_id": 3, "name": "Wei Zhang", "finalGrade": 99.0 } + { "_id": 4, "name": "Juan Gonzalez", "finalGrade": 85.5 } + { "_id": 5, "name": "Erik Trout", "finalGrade": 72.3 } + { "_id": 6, "name": "Demarcus Smith", "finalGrade": 88.8 } + +The following ``Student`` class models the documents in this collection. .. literalinclude:: /includes/code-examples/count-documents/Student.cs - :start-after: start-grades-struct - :end-before: end-grades-struct + :start-after: start-student-struct + :end-before: end-student-struct :language: csharp :dedent: .. note:: - The documents in the ``grades`` collection use the camel-case naming + The documents in the ``students`` collection use the camel-case naming convention. The examples in this guide use a ``ConventionPack`` to deserialize the fields in the collection into Pascal case and map them to the properties in the ``Student`` class. @@ -65,19 +64,18 @@ Example ~~~~~~~ The following example counts the number of documents where the -``finalGrade`` is less than ``80``: +value of ``finalGrade`` is less than ``80``: .. io-code-block:: :copyable: true - .. input:: - :language: csharp + .. literalinclude:: /includes/code-examples/count-documents/Student.cs + :start-after: start-count-guide + :end-before: end-count-guide + :language: csharp + :dedent: - var filter = Builders.Filter.Lt("finalGrade", 80); - var count = coll.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade less - than 80: " + count); .. output:: :language: none @@ -178,7 +176,7 @@ Example ``````` The following example estimates the number of documents in the -``grades`` collection: +``students`` collection: .. io-code-block:: :copyable: true @@ -188,14 +186,14 @@ The following example estimates the number of documents in the var count = collection.EstimatedDocumentCount(); String output = String.Format("Estimated number of documents in - the grades collection: " + count); + the students collection: " + count); Console.WriteLine(output); .. output:: :language: none :visible: false - Estimated number of documents in the grades collection: 6 + Estimated number of documents in the students collection: 6 .. _csharp-count-aggregation: diff --git a/source/includes/code-examples/count-documents/Student.cs b/source/includes/code-examples/count-documents/Student.cs index b2918a16..9f1434cc 100644 --- a/source/includes/code-examples/count-documents/Student.cs +++ b/source/includes/code-examples/count-documents/Student.cs @@ -1,7 +1,17 @@ -//start-grades-struct +//start-count-guide +var filter = Builders.Filter.Lt("finalGrade", 80); + var count = coll.CountDocuments(filter); + + Console.WriteLine("Number of documents with a final grade less + than 80: " + count); +//end-count-guide + + +//start-student-struct public class Student { - public ObjectId Id { get; set; } + public int Id { get; set; } public string Name { get; set; } public double FinalGrade { get; set; } } -//end-grades-struct \ No newline at end of file +//end-student-struct + From b1f104f5b8848868fca0f9bb1495a3fead0f6f6f Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 15:22:19 -0400 Subject: [PATCH 18/28] check before and after --- .../crud/read-operations/count.txt | 9 +-- .../code-examples/count-documents/Student.cs | 17 ----- .../code-examples/CountDocuments.cs | 62 +++++++++++++++++++ 3 files changed, 65 insertions(+), 23 deletions(-) delete mode 100644 source/includes/code-examples/count-documents/Student.cs create mode 100644 source/includes/fundamentals/code-examples/CountDocuments.cs diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index c97fd488..518f8ae0 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -181,14 +181,11 @@ The following example estimates the number of documents in the .. io-code-block:: :copyable: true - .. input:: + .. input:: /source/includes/fundamentals/code-examples/CountDocuments.cs + :start-after: start-est-count + :end-before: end-est-count :language: csharp - var count = collection.EstimatedDocumentCount(); - String output = String.Format("Estimated number of documents in - the students collection: " + count); - Console.WriteLine(output); - .. output:: :language: none :visible: false diff --git a/source/includes/code-examples/count-documents/Student.cs b/source/includes/code-examples/count-documents/Student.cs deleted file mode 100644 index 9f1434cc..00000000 --- a/source/includes/code-examples/count-documents/Student.cs +++ /dev/null @@ -1,17 +0,0 @@ -//start-count-guide -var filter = Builders.Filter.Lt("finalGrade", 80); - var count = coll.CountDocuments(filter); - - Console.WriteLine("Number of documents with a final grade less - than 80: " + count); -//end-count-guide - - -//start-student-struct -public class Student { - public int Id { get; set; } - public string Name { get; set; } - public double FinalGrade { get; set; } -} -//end-student-struct - diff --git a/source/includes/fundamentals/code-examples/CountDocuments.cs b/source/includes/fundamentals/code-examples/CountDocuments.cs new file mode 100644 index 00000000..98f8f18a --- /dev/null +++ b/source/includes/fundamentals/code-examples/CountDocuments.cs @@ -0,0 +1,62 @@ +using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Driver; +namespace TestRun.Fundamentals; +public class CountDocuments +{ + private static IMongoCollection _myColl; + private const string MongoConnectionString = ""; + public static void Main(string[] args) + { + Setup(); + InsertSampleData(); + + // start-accurate-ct + var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); + var count1 = _myColl.CountDocuments(filter); + Console.WriteLine("Number of documents with a final grade less than 80: " + count1); + // end-accurate-ct + + // start-est-count + var count2 = _myColl.EstimatedDocumentCount(); + Console.WriteLine("Estimated number of documents in the students collection: " + count2); + // end-est-count + + // start-agg-count + var matchStage = Builders + .Filter.Gt(s => s.FinalGrade, 80); + var result = _myColl.Aggregate().Match(matchStage).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: "+result.First().Count); + // end-agg-count + + _myColl.DeleteMany(Builders.Filter.Empty); + } + private static void InsertSampleData() + { + var studentList = new List() + { + new() { Id= 1, Name = "Jonathon Howard ", FinalGrade = 87.5 }, + new() { Id= 2, Name = "ABCDEF Howard ", FinalGrade = 10.5 }, + new() { Id= 3, Name = "aeufh Howard ", FinalGrade = 90.5 } + }; + var options = new InsertManyOptions() { BypassDocumentValidation = true }; + _myColl.InsertMany(studentList, options); + } + private static void Setup() + { + // This allows automapping of the camelCase database fields to our models. + var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; + ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); + + // Establish the connection to MongoDB and get the restaurants database + var mongoClient = new MongoClient(MongoConnectionString); + var testDB = mongoClient.GetDatabase("test"); + _myColl = testDB.GetCollection("students"); + } +} +//start-student-struct +public class Student { + public int Id { get; set; } + public string Name { get; set; } + public double FinalGrade { get; set; } +} +// end-student-struct \ No newline at end of file From c7d9eeeb99d3d7e5602ad80f9981d1a2afad1401 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 15:45:28 -0400 Subject: [PATCH 19/28] post review --- .../crud/read-operations/count.txt | 54 +++++++++---------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 518f8ae0..bee183fa 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -70,8 +70,8 @@ value of ``finalGrade`` is less than ``80``: :copyable: true .. literalinclude:: /includes/code-examples/count-documents/Student.cs - :start-after: start-count-guide - :end-before: end-count-guide + :start-after: start-accurate-count + :end-before: end-accurate-count :language: csharp :dedent: @@ -86,12 +86,10 @@ value of ``finalGrade`` is less than ``80``: Modify Behavior ~~~~~~~~~~~~~~~ -You can modify the behavior of ``CountDocuments()`` by passing in a -``CountOptions`` type. If you don't specify any options, the driver uses -its default values. +You can modify the behavior of ``CountDocuments()`` by passing a ``CountOptions`` type as +a parameter. If you don't specify any options, the driver uses default values. -The ``CountOptions`` type allows you to configure options with the -following properties: +You can set the following properties in a ``CountOptions`` object: .. list-table:: :widths: 30 70 @@ -129,10 +127,9 @@ following properties: with an empty query parameter. .. code-block:: csharp - :emphasize-lines: 1, 3 + :emphasize-lines: 1, 2 var filter = Builders.Filter.Empty; - CountOptions opts = new CountOptions(){Hint = "_id_"}; var counter = collection.CountDocuments(filter, opts); @@ -141,8 +138,7 @@ following properties: Estimated Count --------------- -To estimate the number of documents in your collection without the use -of a filter, use the +To estimate the total number of documents in your collection, use the ``EstimatedDocumentCount()`` method. .. note:: @@ -154,12 +150,11 @@ of a filter, use the Modify Behavior ~~~~~~~~~~~~~~~ -You can modify the behavior of ``EstimatedDocumentCount()`` by passing -in an ``EstimatedDocumentCountOptions`` type. If you don't specify any -options, the driver uses its default values. +You can modify the behavior of ``EstimatedDocumentCount()`` by passing a +``EstimatedDocumentCountOptions`` type as a parameter. If you don't +specify any options, the driver uses default values. -The ``EstimatedDocumentCountOptions`` type allows you to configure -options with the following property: +You can set the following properties in a ``EstimatedDocumentCountOptions`` object: .. list-table:: :widths: 30 70 @@ -181,10 +176,12 @@ The following example estimates the number of documents in the .. io-code-block:: :copyable: true - .. input:: /source/includes/fundamentals/code-examples/CountDocuments.cs - :start-after: start-est-count - :end-before: end-est-count + .. input:: :language: csharp + :dedent: + + var count = _myColl.EstimatedDocumentCount(); + Console.WriteLine("Estimated number of documents in the students collection: " + count); .. output:: :language: none @@ -197,16 +194,16 @@ The following example estimates the number of documents in the Aggregation ----------- -You can also include the :manual:`$count ` -stage to count the number of documents in an aggregation pipeline. +You can also include the ``Count()`` builder method to count the number +of documents in an aggregation pipeline. Example ~~~~~~~ The following example performs the following actions: -- Counts the number of documents where the ``finalGrade`` is greater than ``80`` -- Assigns the count to the ``count`` field +- Specifies a filter in the match stage +- Appends a count stage using ``Count()`` .. io-code-block:: :copyable: true @@ -215,17 +212,16 @@ The following example performs the following actions: :language: csharp var matchStage = Builders - .Filter.Gt(f => f.finalGrade, 80); - var pipeline = new EmptyPipelineDefinition() - .Match(matchStage).Count(); - Console.WriteLine(pipeline); + .Filter.Gt(s => s.FinalGrade, 80); + var result = _myColl.Aggregate().Match(matchStage).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); .. output:: :language: none :visible: false - [{ "$match" : { "finalGrade" : { "$gt" : 80.0 } } }, - { "$count" : "count" }] + Number of documents with a final grade more than 80: 2 + Additional Information ---------------------- From b64717aa18876a7e03ce07e19532b4c964b4eabd Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 15:56:57 -0400 Subject: [PATCH 20/28] error fix --- source/fundamentals/crud/read-operations/count.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index bee183fa..dea347d6 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``Student`` class models the documents in this collection. -.. literalinclude:: /includes/code-examples/count-documents/Student.cs +.. literalinclude:: /source/includes/fundamentals/code-examples/CountDocuments.cs :start-after: start-student-struct :end-before: end-student-struct :language: csharp @@ -69,13 +69,13 @@ value of ``finalGrade`` is less than ``80``: .. io-code-block:: :copyable: true - .. literalinclude:: /includes/code-examples/count-documents/Student.cs - :start-after: start-accurate-count - :end-before: end-accurate-count - :language: csharp - :dedent: + .. input:: + :language: csharp + :dedent: - + var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); + var count1 = _myColl.CountDocuments(filter); + Console.WriteLine("Number of documents with a final grade less than 80: " + count1); .. output:: :language: none From 6d8fec035f60f6667642cf8448ae134895db4a99 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 16:00:44 -0400 Subject: [PATCH 21/28] error fix --- source/fundamentals/crud/read-operations/count.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index dea347d6..f2b5847c 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -35,7 +35,7 @@ The examples in this guide use the following documents in a collection called The following ``Student`` class models the documents in this collection. -.. literalinclude:: /source/includes/fundamentals/code-examples/CountDocuments.cs +.. literalinclude:: /includes/fundamentals/code-examples/CountDocuments.cs :start-after: start-student-struct :end-before: end-student-struct :language: csharp From 8bbbe71233cb8231020ccdb2752ca6ddc930b24a Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Wed, 21 Jun 2023 16:54:08 -0400 Subject: [PATCH 22/28] error fix --- .../crud/read-operations/count.txt | 16 +-- .../code-examples/CountDocuments.cs | 98 +++++++++---------- 2 files changed, 57 insertions(+), 57 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index f2b5847c..e28ca5f3 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -74,8 +74,8 @@ value of ``finalGrade`` is less than ``80``: :dedent: var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); - var count1 = _myColl.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade less than 80: " + count1); + var count = _myColl.CountDocuments(filter); + Console.WriteLine("Number of documents with a final grade less than 80: " + count); .. output:: :language: none @@ -180,8 +180,8 @@ The following example estimates the number of documents in the :language: csharp :dedent: - var count = _myColl.EstimatedDocumentCount(); - Console.WriteLine("Estimated number of documents in the students collection: " + count); + var count = _myColl.EstimatedDocumentCount(); + Console.WriteLine("Estimated number of documents in the students collection: " + count); .. output:: :language: none @@ -211,10 +211,10 @@ The following example performs the following actions: .. input:: :language: csharp - var matchStage = Builders - .Filter.Gt(s => s.FinalGrade, 80); - var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); + var matchStage = Builders + .Filter.Gt(s => s.FinalGrade, 80); + var result = _myColl.Aggregate().Match(matchStage).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); .. output:: :language: none diff --git a/source/includes/fundamentals/code-examples/CountDocuments.cs b/source/includes/fundamentals/code-examples/CountDocuments.cs index 98f8f18a..7b4e22dd 100644 --- a/source/includes/fundamentals/code-examples/CountDocuments.cs +++ b/source/includes/fundamentals/code-examples/CountDocuments.cs @@ -3,60 +3,60 @@ namespace TestRun.Fundamentals; public class CountDocuments { - private static IMongoCollection _myColl; - private const string MongoConnectionString = ""; - public static void Main(string[] args) - { - Setup(); - InsertSampleData(); - - // start-accurate-ct - var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); - var count1 = _myColl.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade less than 80: " + count1); - // end-accurate-ct + private static IMongoCollection _myColl; + private const string MongoConnectionString = ""; + public static void Main(string[] args) + { + Setup(); + InsertSampleData(); + + // start-accurate-ct + var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); + var count1 = _myColl.CountDocuments(filter); + Console.WriteLine("Number of documents with a final grade less than 80: " + count1); + // end-accurate-ct - // start-est-count - var count2 = _myColl.EstimatedDocumentCount(); - Console.WriteLine("Estimated number of documents in the students collection: " + count2); - // end-est-count + // start-est-count + var count2 = _myColl.EstimatedDocumentCount(); + Console.WriteLine("Estimated number of documents in the students collection: " + count2); + // end-est-count - // start-agg-count - var matchStage = Builders - .Filter.Gt(s => s.FinalGrade, 80); - var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade more than 80: "+result.First().Count); - // end-agg-count - - _myColl.DeleteMany(Builders.Filter.Empty); - } - private static void InsertSampleData() - { - var studentList = new List() - { - new() { Id= 1, Name = "Jonathon Howard ", FinalGrade = 87.5 }, - new() { Id= 2, Name = "ABCDEF Howard ", FinalGrade = 10.5 }, - new() { Id= 3, Name = "aeufh Howard ", FinalGrade = 90.5 } - }; - var options = new InsertManyOptions() { BypassDocumentValidation = true }; - _myColl.InsertMany(studentList, options); - } - private static void Setup() - { - // This allows automapping of the camelCase database fields to our models. - var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; - ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); + // start-agg-count + var matchStage = Builders + .Filter.Gt(s => s.FinalGrade, 80); + var result = _myColl.Aggregate().Match(matchStage).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: "+result.First().Count); + // end-agg-count + + _myColl.DeleteMany(Builders.Filter.Empty); + } + private static void InsertSampleData() + { + var studentList = new List() + { + new() { Id= 1, Name = "Jonathon Howard ", FinalGrade = 87.5 }, + new() { Id= 2, Name = "ABCDEF Howard ", FinalGrade = 10.5 }, + new() { Id= 3, Name = "aeufh Howard ", FinalGrade = 90.5 } + }; + var options = new InsertManyOptions() { BypassDocumentValidation = true }; + _myColl.InsertMany(studentList, options); + } + private static void Setup() + { + // This allows automapping of the camelCase database fields to our models. + var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; + ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); - // Establish the connection to MongoDB and get the restaurants database - var mongoClient = new MongoClient(MongoConnectionString); - var testDB = mongoClient.GetDatabase("test"); - _myColl = testDB.GetCollection("students"); - } + // Establish the connection to MongoDB and get the restaurants database + var mongoClient = new MongoClient(MongoConnectionString); + var testDB = mongoClient.GetDatabase("test"); + _myColl = testDB.GetCollection("students"); + } } //start-student-struct public class Student { - public int Id { get; set; } - public string Name { get; set; } - public double FinalGrade { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public double FinalGrade { get; set; } } // end-student-struct \ No newline at end of file From 62775d005ae2a634d14f0b41d07d4c3d8a8de7f3 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 10:22:11 -0400 Subject: [PATCH 23/28] error fix --- source/fundamentals/crud/read-operations/count.txt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index e28ca5f3..fea68fa7 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -71,7 +71,6 @@ value of ``finalGrade`` is less than ``80``: .. input:: :language: csharp - :dedent: var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); var count = _myColl.CountDocuments(filter); @@ -178,10 +177,9 @@ The following example estimates the number of documents in the .. input:: :language: csharp - :dedent: - var count = _myColl.EstimatedDocumentCount(); - Console.WriteLine("Estimated number of documents in the students collection: " + count); + var count = _myColl.EstimatedDocumentCount(); + Console.WriteLine("Estimated number of documents in the students collection: " + count); .. output:: :language: none @@ -211,10 +209,10 @@ The following example performs the following actions: .. input:: :language: csharp - var matchStage = Builders + var matchStage = Builders .Filter.Gt(s => s.FinalGrade, 80); - var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); + var result = _myColl.Aggregate().Match(matchStage).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); .. output:: :language: none From 0e94854b53818b0b31e0cf499a2847aa0547d32b Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 10:28:27 -0400 Subject: [PATCH 24/28] error fix --- .../includes/fundamentals/code-examples/CountDocuments.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/includes/fundamentals/code-examples/CountDocuments.cs b/source/includes/fundamentals/code-examples/CountDocuments.cs index 7b4e22dd..c18f5817 100644 --- a/source/includes/fundamentals/code-examples/CountDocuments.cs +++ b/source/includes/fundamentals/code-examples/CountDocuments.cs @@ -55,8 +55,8 @@ private static void Setup() } //start-student-struct public class Student { - public int Id { get; set; } - public string Name { get; set; } - public double FinalGrade { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public double FinalGrade { get; set; } } // end-student-struct \ No newline at end of file From 2bedbccf0428f16990fc9dbbf348cad0d8635d41 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 10:40:19 -0400 Subject: [PATCH 25/28] error fix --- source/fundamentals/crud/read-operations/count.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index fea68fa7..9dc24cf8 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -73,8 +73,8 @@ value of ``finalGrade`` is less than ``80``: :language: csharp var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); - var count = _myColl.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade less than 80: " + count); + var count = _myColl.CountDocuments(filter); + Console.WriteLine("Number of documents with a final grade less than 80: " + count); .. output:: :language: none @@ -179,7 +179,8 @@ The following example estimates the number of documents in the :language: csharp var count = _myColl.EstimatedDocumentCount(); - Console.WriteLine("Estimated number of documents in the students collection: " + count); + Console.WriteLine("Estimated number of documents in + the students collection: " + count); .. output:: :language: none @@ -210,9 +211,9 @@ The following example performs the following actions: :language: csharp var matchStage = Builders - .Filter.Gt(s => s.FinalGrade, 80); + .Filter.Gt(s => s.FinalGrade, 80); var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); + Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); .. output:: :language: none From 7ef0fab0b8b2e620fcbca498317c52e631a12636 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 10:45:21 -0400 Subject: [PATCH 26/28] examples too long fix --- source/fundamentals/crud/read-operations/count.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 9dc24cf8..7b751fd6 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -74,7 +74,8 @@ value of ``finalGrade`` is less than ``80``: var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); var count = _myColl.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade less than 80: " + count); + Console.WriteLine("Number of documents with a final grade + less than 80: " + count); .. output:: :language: none @@ -213,7 +214,8 @@ The following example performs the following actions: var matchStage = Builders .Filter.Gt(s => s.FinalGrade, 80); var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); + Console.WriteLine("Number of documents with a final grade + more than 80: " + result.First().Count); .. output:: :language: none From 469a5de4690ca399c76a4d2230e2ed586e9a6aee Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 10:58:12 -0400 Subject: [PATCH 27/28] changes requested --- .../crud/read-operations/count.txt | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/source/fundamentals/crud/read-operations/count.txt b/source/fundamentals/crud/read-operations/count.txt index 7b751fd6..fea32994 100644 --- a/source/fundamentals/crud/read-operations/count.txt +++ b/source/fundamentals/crud/read-operations/count.txt @@ -33,7 +33,7 @@ The examples in this guide use the following documents in a collection called { "_id": 6, "name": "Demarcus Smith", "finalGrade": 88.8 } The following ``Student`` class models the documents in this -collection. +collection: .. literalinclude:: /includes/fundamentals/code-examples/CountDocuments.cs :start-after: start-student-struct @@ -74,8 +74,7 @@ value of ``finalGrade`` is less than ``80``: var filter = Builders.Filter.Lt(s => s.FinalGrade, 80.0); var count = _myColl.CountDocuments(filter); - Console.WriteLine("Number of documents with a final grade - less than 80: " + count); + Console.WriteLine("Number of documents with a final grade less than 80: " + count); .. output:: :language: none @@ -131,7 +130,7 @@ You can set the following properties in a ``CountOptions`` object: var filter = Builders.Filter.Empty; CountOptions opts = new CountOptions(){Hint = "_id_"}; - var counter = collection.CountDocuments(filter, opts); + var count = collection.CountDocuments(filter, opts); .. _csharp-estimated-count: @@ -143,7 +142,7 @@ To estimate the total number of documents in your collection, use the .. note:: - The ``EstimatedDocumentCount()`` method is quicker than the + The ``EstimatedDocumentCount()`` method is more efficient than the ``CountDocuments()`` method because it uses the collection's metadata rather than scanning the entire collection. @@ -180,8 +179,7 @@ The following example estimates the number of documents in the :language: csharp var count = _myColl.EstimatedDocumentCount(); - Console.WriteLine("Estimated number of documents in - the students collection: " + count); + Console.WriteLine("Estimated number of documents in the students collection: " + count); .. output:: :language: none @@ -194,7 +192,7 @@ The following example estimates the number of documents in the Aggregation ----------- -You can also include the ``Count()`` builder method to count the number +You can use the ``Count()`` builder method to count the number of documents in an aggregation pipeline. Example @@ -202,8 +200,9 @@ Example The following example performs the following actions: -- Specifies a filter in the match stage -- Appends a count stage using ``Count()`` +- Specifies a match stage to find documents with a ``FinalGrade`` value + greater than ``80`` +- Counts the number of documents that match the criteria .. io-code-block:: :copyable: true @@ -211,11 +210,10 @@ The following example performs the following actions: .. input:: :language: csharp - var matchStage = Builders + var filter = Builders .Filter.Gt(s => s.FinalGrade, 80); - var result = _myColl.Aggregate().Match(matchStage).Count(); - Console.WriteLine("Number of documents with a final grade - more than 80: " + result.First().Count); + var result = _myColl.Aggregate().Match(filter).Count(); + Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count); .. output:: :language: none From b77ceaefde4115a598585d25ae00cb9297c53b10 Mon Sep 17 00:00:00 2001 From: Nick LDP Date: Fri, 23 Jun 2023 11:23:28 -0400 Subject: [PATCH 28/28] changes requested --- .../fundamentals/code-examples/CountDocuments.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/includes/fundamentals/code-examples/CountDocuments.cs b/source/includes/fundamentals/code-examples/CountDocuments.cs index c18f5817..2195f963 100644 --- a/source/includes/fundamentals/code-examples/CountDocuments.cs +++ b/source/includes/fundamentals/code-examples/CountDocuments.cs @@ -34,9 +34,12 @@ private static void InsertSampleData() { var studentList = new List() { - new() { Id= 1, Name = "Jonathon Howard ", FinalGrade = 87.5 }, - new() { Id= 2, Name = "ABCDEF Howard ", FinalGrade = 10.5 }, - new() { Id= 3, Name = "aeufh Howard ", FinalGrade = 90.5 } + new() { Id = 1, Name = "Jonathon Howard", FinalGrade = 87.5 }, + new() { Id = 2, Name = "Keisha Freeman", FinalGrade = 12.3 }, + new() { Id = 3, Name = "Wei Zhang", FinalGrade = 99.0 }, + new() { Id = 4, Name = "Juan Gonzalez", FinalGrade = 85.5 }, + new() { Id = 5, Name = "Erik Trout", FinalGrade = 72.3 }, + new() { Id = 6, Name = "Demarcus Smith", FinalGrade = 88.8 } }; var options = new InsertManyOptions() { BypassDocumentValidation = true }; _myColl.InsertMany(studentList, options);