Skip to content

Commit 09f485b

Browse files
committed
DOCSP-51822 Standardize count usage ex
1 parent 61b2f4c commit 09f485b

File tree

3 files changed

+142
-11
lines changed

3 files changed

+142
-11
lines changed

source/crud/query/count.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,64 @@ The following example estimates the number of documents in the
237237

238238
Estimated number of documents in the tea collection: 9
239239

240+
Count Documents Example: Full File
241+
----------------------------------
242+
243+
.. include:: /includes/usage-examples/example-intro.rst
244+
245+
The following example performs the following on the ``restaurants``
246+
collection:
247+
248+
- Approximates the number of documents in the collection
249+
- Counts the number of documents in which the value of the ``cuisine`` is "American"
250+
251+
Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
252+
253+
.. tabs::
254+
255+
.. tab :: Struct
256+
:tabid: structExample
257+
258+
The following code uses structs to approximate the number of documents in
259+
the collection and count the number of documents in which the value of the
260+
``cuisine`` is "American":
261+
262+
.. io-code-block::
263+
:copyable: true
264+
265+
.. input:: /includes/usage-examples/code-snippets/count.go
266+
:language: go
267+
:dedent:
268+
269+
.. output::
270+
:language: none
271+
:visible: false
272+
273+
Estimated number of documents in the restaurants collection: 25359
274+
Number of restaurants with American cuisine: 6183
275+
276+
.. tab :: bson.D
277+
:tabid: bsonDExample
278+
279+
The following code uses a bson.D type to approximate the number of documents in
280+
the collection and count the number of documents in which the value of the
281+
``cuisine`` is "American":
282+
283+
.. io-code-block::
284+
:copyable: true
285+
286+
.. input:: /includes/usage-examples/code-snippets/countBsonD.go
287+
:language: go
288+
:dedent:
289+
290+
.. output::
291+
:language: none
292+
:visible: false
293+
294+
Estimated number of documents in the restaurants collection: 25359
295+
Number of restaurants with American cuisine: 6183
296+
297+
240298
Additional Information
241299
----------------------
242300

source/includes/usage-examples/code-snippets/count.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,29 @@ import (
1313
"go.mongodb.org/mongo-driver/v2/mongo/options"
1414
)
1515

16+
type Restaurant struct {
17+
ID bson.ObjectID `bson:"_id"`
18+
Name string
19+
RestaurantId string `bson:"restaurant_id"`
20+
Cuisine string
21+
Address interface{}
22+
Borough string
23+
Grades interface{}
24+
}
25+
26+
// Creates a filter struct to use for the query
27+
type RestaurantCuisineFilter struct {
28+
Cuisine string
29+
}
30+
1631
func main() {
1732
if err := godotenv.Load(); err != nil {
1833
log.Println("No .env file found")
1934
}
2035

2136
var uri string
2237
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23-
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")
38+
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")
2439
}
2540

2641
client, err := mongo.Connect(options.Client().ApplyURI(uri))
@@ -33,12 +48,11 @@ func main() {
3348
}
3449
}()
3550

36-
// begin countDocuments
37-
coll := client.Database("sample_mflix").Collection("movies")
51+
coll := client.Database("sample_restaurants").Collection("restaurants")
3852

39-
// Specifies a filter to match documents where the "countries" array
40-
// includes a value of "China"
41-
filter := bson.D{{"countries", "China"}}
53+
// Specifies a filter to match documents where the "cuisine"
54+
// has a value of "American"
55+
filter := RestaurantCuisineFilter{Cuisine: "American"}
4256

4357
// Retrieves and prints the estimated number of documents in the collection
4458
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
@@ -52,11 +66,10 @@ func main() {
5266
if err != nil {
5367
panic(err)
5468
}
55-
// end countDocuments
5669

5770
// When you run this file, it should print:
58-
// Estimated number of documents in the movies collection: 23541
59-
// Number of movies from China: 303
60-
fmt.Printf("Estimated number of documents in the movies collection: %d\n", estCount)
61-
fmt.Printf("Number of movies from China: %d\n", count)
71+
// Estimated number of documents in the movies collection: 25359
72+
// Number of restaurants with American cuisine: 6183
73+
fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount)
74+
fmt.Printf("Number of restaurants with American cuisine: %d\n", count)
6275
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Counts documents in a collection by using the Go driver
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
14+
)
15+
16+
func main() {
17+
if err := godotenv.Load(); err != nil {
18+
log.Println("No .env file found")
19+
}
20+
21+
var uri string
22+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23+
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")
24+
}
25+
26+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer func() {
31+
if err = client.Disconnect(context.TODO()); err != nil {
32+
panic(err)
33+
}
34+
}()
35+
36+
coll := client.Database("sample_restaurants").Collection("restaurants")
37+
38+
// Specifies a filter to match documents where the "cuisine"
39+
// has a value of "American"
40+
filter := bson.D{{"cuisine", "American"}}
41+
42+
// Retrieves and prints the estimated number of documents in the collection
43+
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
44+
if estCountErr != nil {
45+
panic(estCountErr)
46+
}
47+
48+
// Retrieves and prints the number of documents in the collection
49+
// that match the filter
50+
count, err := coll.CountDocuments(context.TODO(), filter)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
// When you run this file, it should print:
56+
// Estimated number of documents in the movies collection: 25359
57+
// Number of restaurants with American cuisine: 6183
58+
fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount)
59+
fmt.Printf("Number of restaurants with American cuisine: %d\n", count)
60+
}

0 commit comments

Comments
 (0)