@@ -4,100 +4,51 @@ Find a Document
4
4
5
5
.. default-domain:: mongodb
6
6
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.
9
9
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.
12
13
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
19
15
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
56
22
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.
65
24
66
- Example
67
- -------
25
+ Expected Result
26
+ ---------------
68
27
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:
71
29
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
80
32
81
- .. literalinclude:: findOne.go
82
- :language: go
33
+ {
34
+ ...
35
+ "title": "The Room",
36
+ "year": 2003,
37
+ ...
38
+ }
83
39
84
- If you run the example above, you should see a result that looks like this:
40
+ Additional Information
41
+ ----------------------
85
42
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>**.
88
46
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/>`.
90
50
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
+ ~~~~~~~~~~~~~~~~~
93
53
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