Skip to content

Commit a83e137

Browse files
authored
DOCSP-51822 Standardize count usage ex (#542)
* DOCSP-51822 Standardize count usage ex * NR review and tech feedback
1 parent eec61b1 commit a83e137

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

source/crud/query/count.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ 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+
This example performs the following on the ``restaurants`` collection:
246+
247+
- Approximates the number of documents in the collection
248+
- Counts the number of documents in which the value of the ``cuisine`` field is ``"American"``
249+
250+
.. io-code-block::
251+
:copyable: true
252+
253+
.. input:: /includes/usage-examples/code-snippets/count.go
254+
:language: go
255+
:dedent:
256+
257+
.. output::
258+
:language: none
259+
:visible: false
260+
261+
Estimated number of documents in the restaurants collection: 25359
262+
Number of restaurants with American cuisine: 6183
263+
240264
Additional Information
241265
----------------------
242266

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ 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+
1626
func main() {
1727
if err := godotenv.Load(); err != nil {
1828
log.Println("No .env file found")
1929
}
2030

2131
var uri string
2232
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")
33+
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")
2434
}
2535

2636
client, err := mongo.Connect(options.Client().ApplyURI(uri))
@@ -33,30 +43,27 @@ func main() {
3343
}
3444
}()
3545

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

39-
// Specifies a filter to match documents where the "countries" array
40-
// includes a value of "China"
41-
filter := bson.D{{"countries", "China"}}
48+
// Specifies a filter to match documents where the "cuisine" field
49+
// has a value of "American"
50+
filter := bson.D{{"cuisine", "American"}}
4251

4352
// Retrieves and prints the estimated number of documents in the collection
4453
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
4554
if estCountErr != nil {
4655
panic(estCountErr)
4756
}
4857

49-
// Retrieves and prints the number of documents in the collection
50-
// that match the filter
58+
// Retrieves and prints the number of matching documents in the collection
5159
count, err := coll.CountDocuments(context.TODO(), filter)
5260
if err != nil {
5361
panic(err)
5462
}
55-
// end countDocuments
5663

5764
// 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)
65+
// Estimated number of documents in the movies collection: 25359
66+
// Number of restaurants with American cuisine: 6183
67+
fmt.Printf("Estimated number of documents in the restaurants collection: %d\n", estCount)
68+
fmt.Printf("Number of restaurants with American cuisine: %d\n", count)
6269
}

0 commit comments

Comments
 (0)