Skip to content

DOCSP-51822 Standardize count usage ex #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: comp-cov
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions source/crud/query/count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------

Expand Down
33 changes: 20 additions & 13 deletions source/includes/usage-examples/code-snippets/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ 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")
}

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))
Expand All @@ -33,30 +43,27 @@ 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())
if estCountErr != nil {
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)
}
Loading