Skip to content

Commit

Permalink
feedback pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
norareidy committed Oct 3, 2023
1 parent 85b37b5 commit fab2078
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
41 changes: 20 additions & 21 deletions source/fundamentals/crud/read-operations/retrieve.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The ``Find()`` method expects you to pass a ``Context`` type and a
query filter. The method returns *all* documents that match the filter
as a ``Cursor`` type.

For a ``Find()`` method code example, see the :ref:`golang-find-example`
For an example that uses the ``Find()`` method, see the :ref:`golang-find-example`
section of this page. To learn how to access data by using a cursor, see
the :ref:`golang-cursor` guide.

Expand All @@ -77,12 +77,12 @@ The ``FindOne()`` method expects you to pass a ``Context`` type and a
query filter. The method returns *the first document* that matches the
filter as a ``SingleResult`` type.

For a ``FindOne()`` method code example, see the :ref:`golang-find-one-example`
section of this page. For a ``FindOne()`` code example that queries by using
a specific ``ObjectId`` value, see the :ref:`golang-find-one-by-id` section
of this page.
For an example that uses the ``FindOne()`` method, see the
:ref:`golang-find-one-example` section of this page. For an example that
uses ``FindOne()`` and queries by using a specific ``ObjectId`` value, see
the :ref:`golang-find-one-by-id` section of this page.

To learn how to access data in a ``SingleResult``, see
To learn how to access data from a ``SingleResult`` type, see
:ref:`golang-bson-unmarshalling` in the BSON guide.

.. _golang-retrieve-options:
Expand Down Expand Up @@ -191,27 +191,26 @@ Find One by ObjectId Example
````````````````````````````

This example defines an ``id`` variable with a value of type ``ObjectId``
and specifies a query filter with the ``id`` variable. The value of ``id``
corresponds to the ``_id`` field value of the following document:
and uses ``id`` to specify a query filter. The filter matches a document
with an ``_id`` field value that corresponds to the ``id`` variable.
This example queries for the following document based on its ``_id`` value:

.. code-block:: json
:copyable: false
:emphasize-lines: 3

[
{
_id: ObjectId('65170b42b99efdd0b07d42de'),
item: "Hibiscus",
rating : 4,
date_ordered : 2009-12-18T05:00:00.000+00:00
}
]
{
_id: ObjectId('65170b42b99efdd0b07d42de'),
item: "Hibiscus",
rating : 4,
date_ordered : 2009-12-18T05:00:00.000+00:00
}

The ``FindOne()`` method recieves the filter and a ``FindOneOptions``
instance as parameters and performs the following actions:
The following code passes the filter and a ``FindOneOptions`` instance
as parameters to the ``FindOne()`` method to perform the following actions:

- Matches the document with the specified ``ObjectId`` value
- Projects the ``Item`` and ``Rating`` fields of the matched document
- Match the document with the specified ``ObjectId`` value
- Project only the ``Item`` and ``Rating`` fields of the matched document

.. io-code-block::
:copyable: true
Expand All @@ -226,7 +225,7 @@ instance as parameters and performs the following actions:
:language: none
:visible: false

[{"Key":"item","Value":"Hibiscus"},{"Key":"rating","Value":4}]
{"item":"Hibiscus","rating":4}

.. note::

Expand Down
25 changes: 12 additions & 13 deletions source/includes/fundamentals/code-snippets/CRUD/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// start-review-struct
type Review struct {
Item string
Rating int32
DateOrdered time.Time `bson:"date_ordered"`
Item string `bson:"item,omitempty"`
Rating int32 `bson:"rating,omitempty"`
DateOrdered time.Time `bson:"date_ordered,omitempty"`
}

// end-review-struct
Expand Down Expand Up @@ -111,23 +112,21 @@ func main() {
if err != nil {
panic(err)
}

filter := bson.D{{"_id", id}}
opts := options.FindOne().SetProjection(
bson.D{{"item",1}, {"rating",1}, {"_id", 0}}
)

var result bson.D
err = coll.FindOne(context.TODO(), filter).Decode(&result)
opts := options.FindOne().SetProjection(bson.D{{"item", 1}, {"rating", 1}, {"_id", 0}})

var result Review
err = coll.FindOne(context.TODO(), filter, opts).Decode(&result)
if err != nil {
panic(err)
}
res, _ := json.Marshal(result)

res, _ := bson.MarshalExtJSON(result, false, false)
fmt.Println(string(res))
// end objectid
}

fmt.Println("\nAggregation:\n")
{
// begin aggregate docs
Expand Down

0 comments on commit fab2078

Please sign in to comment.