diff --git a/source/fundamentals/crud/read-operations/retrieve.txt b/source/fundamentals/crud/read-operations/retrieve.txt index 52e7290b..58a886df 100644 --- a/source/fundamentals/crud/read-operations/retrieve.txt +++ b/source/fundamentals/crud/read-operations/retrieve.txt @@ -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. @@ -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: @@ -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 @@ -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:: diff --git a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go index f44c9dba..1be7fd4f 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go +++ b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go @@ -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 @@ -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