diff --git a/source/crud/query/count.txt b/source/crud/query/count.txt index 6c1ce856..f2a77d83 100644 --- a/source/crud/query/count.txt +++ b/source/crud/query/count.txt @@ -237,6 +237,30 @@ 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 + +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`` field 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 + Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/count.go b/source/includes/usage-examples/code-snippets/count.go index b2a81fdf..6cafaab0 100644 --- a/source/includes/usage-examples/code-snippets/count.go +++ b/source/includes/usage-examples/code-snippets/count.go @@ -13,6 +13,16 @@ 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{} +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -20,7 +30,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 +43,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" field + // 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()) @@ -46,17 +55,15 @@ 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) } - // 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) }