Skip to content

Commit bf8237b

Browse files
DOCSP-13866: update findOne to new format (#33)
* wip * DOCSP-13866: update findOne to new format * remove python2 intersphinx * clean up info * Update source/usage-examples/findOne.txt Co-authored-by: kyuan-mongodb <[email protected]> * wip Co-authored-by: kyuan-mongodb <[email protected]>
1 parent 4c6da3e commit bf8237b

File tree

5 files changed

+85
-132
lines changed

5 files changed

+85
-132
lines changed

snooty.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ toc_landing_pages = [
99
intersphinx = [
1010
"https://docs.mongodb.com/manual/objects.inv",
1111
"https://docs.atlas.mongodb.com/objects.inv",
12-
"https://docs.python.org/2/python2.inv"
1312
]
1413

1514
[constants]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"go.mongodb.org/mongo-driver/bson"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/mongo/options"
13+
)
14+
15+
func main() {
16+
var uri string
17+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
18+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
19+
}
20+
21+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer func() {
26+
if err = client.Disconnect(context.TODO()); err != nil {
27+
panic(err)
28+
}
29+
}()
30+
31+
// begin findOne
32+
coll := client.Database("sample_mflix").Collection("movies")
33+
var result bson.D
34+
err = coll.FindOne(context.TODO(), bson.D{{"title", "The Room"}}).Decode(&result)
35+
// end findOne
36+
if err != nil {
37+
if err == mongo.ErrNoDocuments {
38+
// This error means your query did not match any documents.
39+
return
40+
}
41+
panic(err)
42+
}
43+
// use Map() to quickly convert bson.D to a map
44+
output, err := json.MarshalIndent(result.Map(), "", " ")
45+
if err != nil {
46+
panic(err)
47+
}
48+
fmt.Printf("%s\n", output)
49+
}

source/usage-examples/find.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ which matches documents in the ``zips`` collection where the value of the
2020
:language: go
2121
:dedent:
2222

23-
Click here **<TODO: github link to file>** to see a fully runnable example.
23+
Click `here <{+example+}/find.go>`__ to see a fully runnable example.
2424

2525
Expected Result
2626
---------------

source/usage-examples/findOne.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

source/usage-examples/findOne.txt

Lines changed: 35 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,51 @@ Find a Document
44

55
.. default-domain:: mongodb
66

7-
You can retrieve a single document from your collection with the
8-
``Collection.FindOne()`` method.
7+
You can retrieve a single document from a collection by using the
8+
``FindOne()`` method.
99

10-
..
11-
method
10+
The following example passes a query filter to the ``FindOne()`` method,
11+
which matches documents in the ``movies`` collection where the value
12+
of the ``title`` field is *The Room*, returning the first document found.
1213

13-
The ``FindOne()`` method takes a filter document. Calling the ``FindOne()``
14-
method retrieves a document from your MongoDB instance that matches your
15-
filter document. If you pass the ``FindOne()`` method an empty filter document,
16-
MongoDB considers your entire collection when retrieving your document. For more
17-
information on documents in the MongoDB Go driver, see our
18-
:doc:`guide on data formats </fundamentals/data-formats>`.
14+
.. include:: /includes/usage-examples/run-example-tip.rst
1915

20-
You must pass the ``FindOne()`` method a ``context.Context`` interface type
21-
value. For more information on the ``context.Context`` interface and the
22-
``context`` package, see our :doc:`guide on contexts </fundamentals/context>`.
23-
24-
..
25-
options
26-
27-
You can specify options that change the behavior of the ``FindOne()`` method.
28-
One example of an option is **sort**, which instructs your MongoDB instance to
29-
order the matching documents of your collection before it retrieves the
30-
first document. Another example of an option is a **projection**, which
31-
specifies or restricts the fields in your returned document. You can specify
32-
options by passing the ``FindOne()`` method a pointer to a ``FindOneOptions`` struct.
33-
You can configure a ``FindOneOptions`` struct by using setter methods such as the
34-
``FindOneOptions.SetProjection()`` method. For more information on the sort option,
35-
:doc:`see our guide on sorting </fundamentals/crud/read-operations/sort/>`.
36-
For more information on the projection option,
37-
<TODO:>.
38-
39-
..
40-
return
41-
42-
If the query filter you specified in your ``FindOne()`` method call matches one
43-
or more documents in your collection, it returns the first matched result. The
44-
return type ``SingleResult`` represents a single document returned from a query
45-
to a MongoDB instance. You can call the ``SingleResult.Decode()`` method
46-
to unmarshal a ``SingleResult`` struct type into a variable. Pass the ``Decode()``
47-
method a pointer to the variable that you would like to contain your returned
48-
document.
49-
50-
..
51-
error
52-
53-
When you call the ``Decode()`` method to unmarshal your result, the method
54-
returns an ``error`` interface type which can contain one of the following
55-
values:
16+
.. literalinclude:: /includes/usage-examples/code-snippets/findOne.go
17+
:start-after: begin findOne
18+
:end-before: end findOne
19+
:language: go
20+
:dedent:
21+
:emphasize-lines: 3
5622

57-
- ``nil`` if a document matched your query, and there were no errors retrieving
58-
the document.
59-
- ``ErrNoDocuments`` if no documents matched your query.
60-
- If the driver retrieved your document but could not unmarshal your result, the
61-
``Decode()`` method returns the unmarshaling error.
62-
- If there was an error retrieving your document during execution of the
63-
``FindOne()`` method, the error propagates to the ``Decode()`` method and
64-
the ``Decode()`` method returns the error.
23+
Click `here <{+example+}/findOne.go>`__ to see a fully runnable example.
6524

66-
Example
67-
-------
25+
Expected Result
26+
---------------
6827

69-
The following code example retrieves a single document from the ``movies``
70-
collection. It uses the following arguments for the ``FindOne()`` method:
28+
After you run the code example, you should see output similar to:
7129

72-
- An empty **Context** instance returned from the ``context.TODO()`` function.
73-
- A **filter document** that only matches movies with the title exactly matching
74-
the text ``"The Room"``.
75-
- **Options** that specify a **sort** of matched documents in
76-
descending order by rating, such that the first document has the highest
77-
rating. The options also specify a **projection** for the results so
78-
that the MongoDB instance only sends the ``title`` and ``imdb`` fields of
79-
the retrieved document.
30+
.. code-block:: json
31+
:copyable: False
8032

81-
.. literalinclude:: findOne.go
82-
:language: go
33+
{
34+
...
35+
"title": "The Room",
36+
"year": 2003,
37+
...
38+
}
8339

84-
If you run the example above, you should see a result that looks like this:
40+
Additional Information
41+
----------------------
8542

86-
.. literalinclude:: findOne-output.txt
87-
:language: none
43+
For more information on specifying query filters and
44+
handling potential errors, see our guide on **<TODO: retrieve data
45+
fundamental page>**.
8846

89-
.. include:: /includes/sample-data-note.rst
47+
For more information on query operators,
48+
see the :manual:`MongoDB query operator reference documentation
49+
</reference/operator/query/>`.
9050

91-
For more information on the methods, interfaces, structs, and functions
92-
mentioned on this page, see the following API documentation:
51+
API Documentation
52+
~~~~~~~~~~~~~~~~~
9353

94-
- :go-api:`Collection.FindOne() <mongo#Collection.FindOne>`
95-
- `Context <https://pkg.go.dev/context#Context>`__
96-
- :go-api:`FindOneOptions <mongo/options#FindOneOptions>`
97-
- :go-api:`FindOneOptions.SetProjection() <mongo/options#FindOneOptions.SetProjection>`
98-
- :go-api:`options.FindOne() <mongo/options#FindOne>`
99-
- :go-api:`SingleResult <mongo#SingleResult>`
100-
- :go-api:`SingleResult.Decode() <mongo#SingleResult.Decode>`
101-
- `context.TODO() <https://pkg.go.dev/context#TODO>`__
102-
- :go-api:`bson.D <bson#D>`
103-
- :go-api:`bson.MarshalExtJSON() <bson#MarshalExtJSON>`
54+
`FindOne() <{+godocs+}/mongo#Collection.FindOne>`__

0 commit comments

Comments
 (0)