Skip to content

Commit ee58058

Browse files
DOCSP-13864 count documents usage example (#21)
* added count documents usage example
1 parent 277191c commit ee58058

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/options"
12+
)
13+
14+
func main() {
15+
var uri string
16+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
18+
}
19+
20+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
21+
if err != nil {
22+
panic(err)
23+
}
24+
defer func() {
25+
if err = client.Disconnect(context.TODO()); err != nil {
26+
panic(err)
27+
}
28+
}()
29+
30+
// begin countDocuments
31+
coll := client.Database("sample_mflix").Collection("movies")
32+
filter := bson.D{{"countries", "China"}}
33+
34+
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
35+
count, countErr := coll.CountDocuments(context.TODO(), filter)
36+
// end countDocuments
37+
38+
if estCountErr != nil {
39+
panic(estCountErr)
40+
}
41+
if countErr != nil {
42+
panic(countErr)
43+
}
44+
45+
// When you run this file, it should print:
46+
// Estimated number of documents in the movies collection: 23541
47+
// Number of movies from China: 303
48+
49+
fmt.Printf("Estimated number of documents in the movies collection: %d\n", estCount)
50+
fmt.Printf("Number of movies from China: %d\n", count)
51+
}

source/usage-examples.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Usage Examples
1515
/usage-examples/find-operations
1616
/usage-examples/update-operations
1717
/usage-examples/delete-operations
18-
18+
/usage-examples/count
19+
1920
..
2021
/usage-examples/insert-operations
2122
/usage-examples/bulkWrite
2223
/usage-examples/watch
23-
/usage-examples/count
2424
/usage-examples/distinct
2525
/usage-examples/command
2626

source/usage-examples/count.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,49 @@ Count Documents
33
===============
44

55
.. default-domain:: mongodb
6+
7+
You can get an approximation on the number of documents in a
8+
collection using the ``EstimatedDocumentCount()`` method and an exact
9+
number of documents in a collection using the ``CountDocuments()``
10+
method.
11+
12+
The following example uses the ``EstimatedDocumentCount()`` method to
13+
count the number of documents in the ``movies`` collection. Then, the
14+
example uses the ``CountDocuments()`` method to count the number of
15+
documents in the ``movies`` collection where the ``countries`` field
16+
contains "China".
17+
18+
.. include:: /includes/usage-examples/run-example-tip.rst
19+
20+
.. literalinclude:: /includes/usage-examples/code-snippets/count.go
21+
:start-after: begin countDocuments
22+
:end-before: end countDocuments
23+
:emphasize-lines: 4-5
24+
:language: go
25+
:dedent:
26+
27+
Click `here <{+example+}/count.go>`__ to see a fully runnable example.
28+
29+
Expected Result
30+
---------------
31+
32+
After running the preceding code snippet, you should see the following:
33+
34+
- There are about ``23541`` documents in the ``movies`` collection
35+
- There are ``303`` documents in the ``movies`` collection from "China"
36+
37+
.. note::
38+
39+
The exact number of documents may vary depending on your data set.
40+
41+
Additional Information
42+
----------------------
43+
44+
For more information on counting documents, see our guide on
45+
<TODO: Counting Documents>.
46+
47+
API Documentation
48+
~~~~~~~~~~~~~~~~~
49+
50+
- `CountDocuments() <{+godocs+}/mongo#Collection.CountDocuments>`__
51+
- `EstimatedDocumentCount() <{+godocs+}/mongo#Collection.EstimatedDocumentCount>`__

0 commit comments

Comments
 (0)