Skip to content

Commit 5016d29

Browse files
authored
Merge pull request #8 from rustagir/DOCSP-13856-find-usage-ex
DOCSP 13856: find usage example
2 parents 9dd2d63 + a0862d3 commit 5016d29

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

source/code-snippets/find.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
// Replace the uri string with your MongoDB deployment's connection string.
13+
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
14+
15+
func main() {
16+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
17+
18+
if err != nil {
19+
panic(err)
20+
}
21+
defer func() {
22+
if err = client.Disconnect(context.TODO()); err != nil {
23+
panic(err)
24+
}
25+
}()
26+
27+
// begin find
28+
myCollection := client.Database("sample_training").Collection("zips")
29+
filter := bson.D{{"pop", bson.D{{"$lte", 500}}}}
30+
31+
cursor, err := myCollection.Find(context.TODO(), filter)
32+
// end find
33+
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
var results []bson.D
39+
if err = cursor.All(context.TODO(), &results); err != nil {
40+
panic(err)
41+
}
42+
for _, result := range results {
43+
fmt.Println(result)
44+
}
45+
46+
}

source/usage-examples/find.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,54 @@ Find Multiple Documents
33
=======================
44

55
.. default-domain:: mongodb
6+
7+
You can find multiple documents in a collection by using the ``Find()``
8+
method.
9+
10+
The following example passes a query filter to the ``Find()`` method,
11+
which matches documents in the ``zips`` collection where the value of the
12+
``pop`` field is less than or equal to *500*:
13+
14+
.. include:: /includes/run-example-tip.rst
15+
16+
.. literalinclude:: /code-snippets/find.go
17+
:start-after: begin find
18+
:end-before: end find
19+
:emphasize-lines: 4
20+
:language: go
21+
:dedent:
22+
23+
Click here **<TODO: github link to file>** to see a fully runnable example.
24+
25+
Expected Result
26+
---------------
27+
28+
This example creates a ``Cursor`` object that returns
29+
documents similar to these:
30+
31+
.. code-block:: json
32+
:copyable: false
33+
34+
// results truncated
35+
...
36+
{ ... , "city" : "SHAKTOOLIK", ... , "pop" : 183, "state" : "AK" },
37+
{ ... , "city" : "DARLINGTON", ... , "pop" : 12, "state" : "ID" },
38+
{ ... , "city" : "EAST BARRE", ... , "pop" : 381, "state" : "VT" },
39+
...
40+
41+
Additional Information
42+
----------------------
43+
44+
For more information on specifying query filters and
45+
handling potential errors, see our guide on **<TODO: retrieve data
46+
fundamental page>**.
47+
48+
For more information on query operators,
49+
see the :manual:`MongoDB query operator reference documentation
50+
</reference/operator/query/>`.
51+
52+
API Documentation
53+
~~~~~~~~~~~~~~~~~
54+
55+
- `Find() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.Find>`_
56+
- `Cursor <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Cursor>`_

0 commit comments

Comments
 (0)