From 09f485b5616ca5c29b0f026f34fe0ffba4008bc9 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 18 Jul 2025 19:59:27 -0400 Subject: [PATCH 1/2] DOCSP-51822 Standardize count usage ex --- source/crud/query/count.txt | 58 ++++++++++++++++++ .../usage-examples/code-snippets/count.go | 35 +++++++---- .../code-snippets/countBsonD.go | 60 +++++++++++++++++++ 3 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 source/includes/usage-examples/code-snippets/countBsonD.go diff --git a/source/crud/query/count.txt b/source/crud/query/count.txt index 6c1ce856..4ac6b9d0 100644 --- a/source/crud/query/count.txt +++ b/source/crud/query/count.txt @@ -237,6 +237,64 @@ The following example estimates the number of documents in the Estimated number of documents in the tea collection: 9 +Count Documents Example: Full File +---------------------------------- + +.. include:: /includes/usage-examples/example-intro.rst + +The following example performs the following on the ``restaurants`` +collection: + +- Approximates the number of documents in the collection +- Counts the number of documents in which the value of the ``cuisine`` is "American" + +Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: + +.. tabs:: + + .. tab :: Struct + :tabid: structExample + + The following code uses structs to approximate the number of documents in + the collection and count the number of documents in which the value of the + ``cuisine`` is "American": + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/count.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Estimated number of documents in the restaurants collection: 25359 + Number of restaurants with American cuisine: 6183 + + .. tab :: bson.D + :tabid: bsonDExample + + The following code uses a bson.D type to approximate the number of documents in + the collection and count the number of documents in which the value of the + ``cuisine`` is "American": + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/countBsonD.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Estimated number of documents in the restaurants collection: 25359 + Number of restaurants with American cuisine: 6183 + + Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/count.go b/source/includes/usage-examples/code-snippets/count.go index b2a81fdf..aa73cbe6 100644 --- a/source/includes/usage-examples/code-snippets/count.go +++ b/source/includes/usage-examples/code-snippets/count.go @@ -13,6 +13,21 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) +type Restaurant struct { + ID bson.ObjectID `bson:"_id"` + Name string + RestaurantId string `bson:"restaurant_id"` + Cuisine string + Address interface{} + Borough string + Grades interface{} +} + +// Creates a filter struct to use for the query +type RestaurantCuisineFilter struct { + Cuisine string +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -20,7 +35,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) @@ -33,12 +48,11 @@ func main() { } }() - // begin countDocuments - coll := client.Database("sample_mflix").Collection("movies") + coll := client.Database("sample_restaurants").Collection("restaurants") - // Specifies a filter to match documents where the "countries" array - // includes a value of "China" - filter := bson.D{{"countries", "China"}} + // Specifies a filter to match documents where the "cuisine" + // has a value of "American" + filter := RestaurantCuisineFilter{Cuisine: "American"} // Retrieves and prints the estimated number of documents in the collection estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO()) @@ -52,11 +66,10 @@ func main() { if err != nil { panic(err) } - // end countDocuments // When you run this file, it should print: - // Estimated number of documents in the movies collection: 23541 - // Number of movies from China: 303 - fmt.Printf("Estimated number of documents in the movies collection: %d\n", estCount) - fmt.Printf("Number of movies from China: %d\n", count) + // Estimated number of documents in the movies collection: 25359 + // Number of restaurants with American cuisine: 6183 + fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount) + fmt.Printf("Number of restaurants with American cuisine: %d\n", count) } diff --git a/source/includes/usage-examples/code-snippets/countBsonD.go b/source/includes/usage-examples/code-snippets/countBsonD.go new file mode 100644 index 00000000..53994a1e --- /dev/null +++ b/source/includes/usage-examples/code-snippets/countBsonD.go @@ -0,0 +1,60 @@ +// Counts documents in a collection by using the Go driver +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" +) + +func main() { + if err := godotenv.Load(); err != nil { + log.Println("No .env file found") + } + + var uri string + if uri = os.Getenv("MONGODB_URI"); uri == "" { + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + } + + client, err := mongo.Connect(options.Client().ApplyURI(uri)) + if err != nil { + panic(err) + } + defer func() { + if err = client.Disconnect(context.TODO()); err != nil { + panic(err) + } + }() + + coll := client.Database("sample_restaurants").Collection("restaurants") + + // Specifies a filter to match documents where the "cuisine" + // has a value of "American" + filter := bson.D{{"cuisine", "American"}} + + // Retrieves and prints the estimated number of documents in the collection + estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO()) + if estCountErr != nil { + panic(estCountErr) + } + + // Retrieves and prints the number of documents in the collection + // that match the filter + count, err := coll.CountDocuments(context.TODO(), filter) + if err != nil { + panic(err) + } + + // When you run this file, it should print: + // Estimated number of documents in the movies collection: 25359 + // Number of restaurants with American cuisine: 6183 + fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount) + fmt.Printf("Number of restaurants with American cuisine: %d\n", count) +} From efde7e63f062151576f9270f0bf8a32f2da71578 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 25 Jul 2025 18:09:52 -0400 Subject: [PATCH 2/2] NR review and tech feedback --- source/crud/query/count.txt | 58 ++++-------------- .../usage-examples/code-snippets/count.go | 12 +--- .../code-snippets/countBsonD.go | 60 ------------------- 3 files changed, 15 insertions(+), 115 deletions(-) delete mode 100644 source/includes/usage-examples/code-snippets/countBsonD.go diff --git a/source/crud/query/count.txt b/source/crud/query/count.txt index 4ac6b9d0..f2a77d83 100644 --- a/source/crud/query/count.txt +++ b/source/crud/query/count.txt @@ -242,58 +242,24 @@ Count Documents Example: Full File .. include:: /includes/usage-examples/example-intro.rst -The following example performs the following on the ``restaurants`` -collection: +This example performs the following on the ``restaurants`` collection: - Approximates the number of documents in the collection -- Counts the number of documents in which the value of the ``cuisine`` is "American" +- Counts the number of documents in which the value of the ``cuisine`` field is ``"American"`` -Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: - -.. tabs:: - - .. tab :: Struct - :tabid: structExample - - The following code uses structs to approximate the number of documents in - the collection and count the number of documents in which the value of the - ``cuisine`` is "American": - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/count.go - :language: go - :dedent: - - .. output:: - :language: none - :visible: false - - Estimated number of documents in the restaurants collection: 25359 - Number of restaurants with American cuisine: 6183 - - .. tab :: bson.D - :tabid: bsonDExample - - The following code uses a bson.D type to approximate the number of documents in - the collection and count the number of documents in which the value of the - ``cuisine`` is "American": - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/countBsonD.go - :language: go - :dedent: +.. io-code-block:: + :copyable: true - .. output:: - :language: none - :visible: false + .. input:: /includes/usage-examples/code-snippets/count.go + :language: go + :dedent: - Estimated number of documents in the restaurants collection: 25359 - Number of restaurants with American cuisine: 6183 + .. output:: + :language: none + :visible: false + Estimated number of documents in the restaurants collection: 25359 + Number of restaurants with American cuisine: 6183 Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/count.go b/source/includes/usage-examples/code-snippets/count.go index aa73cbe6..6cafaab0 100644 --- a/source/includes/usage-examples/code-snippets/count.go +++ b/source/includes/usage-examples/code-snippets/count.go @@ -23,11 +23,6 @@ type Restaurant struct { Grades interface{} } -// Creates a filter struct to use for the query -type RestaurantCuisineFilter struct { - Cuisine string -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -50,9 +45,9 @@ func main() { coll := client.Database("sample_restaurants").Collection("restaurants") - // Specifies a filter to match documents where the "cuisine" + // Specifies a filter to match documents where the "cuisine" field // has a value of "American" - filter := RestaurantCuisineFilter{Cuisine: "American"} + filter := bson.D{{"cuisine", "American"}} // Retrieves and prints the estimated number of documents in the collection estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO()) @@ -60,8 +55,7 @@ func main() { panic(estCountErr) } - // Retrieves and prints the number of documents in the collection - // that match the filter + // Retrieves and prints the number of matching documents in the collection count, err := coll.CountDocuments(context.TODO(), filter) if err != nil { panic(err) diff --git a/source/includes/usage-examples/code-snippets/countBsonD.go b/source/includes/usage-examples/code-snippets/countBsonD.go deleted file mode 100644 index 53994a1e..00000000 --- a/source/includes/usage-examples/code-snippets/countBsonD.go +++ /dev/null @@ -1,60 +0,0 @@ -// Counts documents in a collection by using the Go driver -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - if err := godotenv.Load(); err != nil { - log.Println("No .env file found") - } - - var uri string - if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") - } - - client, err := mongo.Connect(options.Client().ApplyURI(uri)) - if err != nil { - panic(err) - } - defer func() { - if err = client.Disconnect(context.TODO()); err != nil { - panic(err) - } - }() - - coll := client.Database("sample_restaurants").Collection("restaurants") - - // Specifies a filter to match documents where the "cuisine" - // has a value of "American" - filter := bson.D{{"cuisine", "American"}} - - // Retrieves and prints the estimated number of documents in the collection - estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO()) - if estCountErr != nil { - panic(estCountErr) - } - - // Retrieves and prints the number of documents in the collection - // that match the filter - count, err := coll.CountDocuments(context.TODO(), filter) - if err != nil { - panic(err) - } - - // When you run this file, it should print: - // Estimated number of documents in the movies collection: 25359 - // Number of restaurants with American cuisine: 6183 - fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount) - fmt.Printf("Number of restaurants with American cuisine: %d\n", count) -}