-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP 13856: find usage example #8
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1577630
DOCSP-13856: find usage example
rustagir 85e7f3a
changes to expected result section
rustagir cff66be
PR fixes 1
rustagir 86c5f96
standardize error handling
rustagir 932f40f
moved uri declaration
rustagir a0862d3
ext review fixes 1
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// Replace the uri string with your MongoDB deployment's connection string. | ||
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority" | ||
|
||
func main() { | ||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
if err = client.Disconnect(context.TODO()); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
// begin find | ||
myCollection := client.Database("sample_training").Collection("zips") | ||
filter := bson.D{{"pop", bson.D{{"$lte", 500}}}} | ||
|
||
cursor, err := myCollection.Find(context.TODO(), filter) | ||
// end find | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var results []bson.M | ||
if err = cursor.All(context.TODO(), &results); err != nil { | ||
panic(err) | ||
} | ||
for _, result := range results { | ||
fmt.Println(result) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.. tip:: | ||
|
||
Read the :doc:`Usage Examples guide </usage-examples>` to learn how | ||
to run this example. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,54 @@ Find Multiple Documents | |
======================= | ||
|
||
.. default-domain:: mongodb | ||
|
||
You can find multiple documents in a collection by using the ``Find()`` | ||
method. | ||
|
||
The following example passes a query filter to the ``Find()`` method, | ||
which matches documents in the ``zips`` collection where the value of the | ||
``pop`` field is less than or equal to *500*: | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. include:: /includes/run-example-tip.rst | ||
|
||
.. literalinclude:: /code-snippets/find.go | ||
:start-after: begin find | ||
:end-before: end find | ||
:emphasize-lines: 4 | ||
:language: go | ||
:dedent: | ||
|
||
Click here **<TODO: github link to file>** to see a fully runnable example. | ||
|
||
Expected Result | ||
--------------- | ||
|
||
This example creates a ``Cursor`` object that returns | ||
documents similar to these: | ||
|
||
.. code-block:: json | ||
:copyable: false | ||
|
||
// results truncated | ||
... | ||
{ ... , "city" : "SHAKTOOLIK", ... , "pop" : 183, "state" : "AK" }, | ||
{ ... , "city" : "DARLINGTON", ... , "pop" : 12, "state" : "ID" }, | ||
{ ... , "city" : "EAST BARRE", ... , "pop" : 381, "state" : "VT" }, | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
For more information on specifying query filters and | ||
handling potential errors, see our guide on **<TODO: retrieve data | ||
fundamental page>**. | ||
|
||
For more information on query operators, | ||
see the :manual:`MongoDB query operator reference documentation | ||
</reference/operator/query/>`. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
- `Find() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.Find>`_ | ||
- `Cursor <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Cursor>`_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.