Skip to content

DOCSP-52409 Remove struct filter from update usage ex #550

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 27 additions & 72 deletions source/crud/update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,94 +209,49 @@ UpdateOne() Example: Full File

.. include:: /includes/usage-examples/example-intro.rst

The following example is a fully runnable file that finds and updates an
existing document in the ``restaurants`` collection. Select the
:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
The following example is a fully runnable file that performs the following
actions on the ``restaurants`` collection:

.. tabs::
- Finds a document with a specific ``_id``
- Updates the ``avg_rating`` field value of the matched document

.. tab:: Struct
:tabid: structExample

The following code uses a struct to define the filter and update a document in the ``restuarants`` collection:

.. io-code-block::
:copyable: true

.. input:: /includes/usage-examples/code-snippets/updateOne.go
:language: go
:dedent:

.. output::
:language: none
:visible: false

Documents updated: 1

.. tab:: bson.D
:tabid: bsonDExample

The following code uses a ``bson.D`` type to define the filter and update a document in the ``restuarants`` collection:

.. io-code-block::
:copyable: true
.. io-code-block::
:copyable: true

.. input:: /includes/usage-examples/code-snippets/updateOneBson.go
:language: go
:dedent:
.. input:: /includes/usage-examples/code-snippets/updateOne.go
:language: go
:dedent:

.. output::
:language: none
:visible: false
.. output::
:language: none
:visible: false

Documents updated: 1
Documents updated: 1

UpdateMany() Example: Full File
-------------------------------

.. include:: /includes/usage-examples/example-intro.rst

The following example is a fully runnable file that finds and updates multiple
existing documents in the ``restaurants`` collection. Select the
:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
The following example is a fully runnable file that performs the following
actions on the ``restaurants`` collection:

.. tabs::
- Finds documents with the ``cuisine`` field value of ``"Pizza"`` and the
``borough`` field value of ``"Brooklyn"``
- Updates the ``avg_rating`` field value of the matched documents

.. tab:: Struct
:tabid: structExample

The following code uses a struct to define the filter and update multiple documents in the ``restuarants`` collection:

.. io-code-block::
:copyable: true

.. input:: /includes/usage-examples/code-snippets/updateMany.go
:language: go
:dedent:

.. output::
:language: none
:visible: false

Documents updated: 296

.. tab:: bson.D
:tabid: bsonDExample

The following code uses a ``bson.D`` type to define the filter and update multiple documents in the ``restuarants`` collection:

.. io-code-block::
:copyable: true
.. io-code-block::
:copyable: true

.. input:: /includes/usage-examples/code-snippets/updateManyBson.go
:language: go
:dedent:
.. input:: /includes/usage-examples/code-snippets/updateMany.go
:language: go
:dedent:

.. output::
:language: none
:visible: false
.. output::
:language: none
:visible: false

Documents updated: 296
Documents updated: 296

Additional Information
----------------------
Expand Down
Empty file.
20 changes: 3 additions & 17 deletions source/includes/usage-examples/code-snippets/updateMany.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ type Restaurant struct {
AverageRating float64 `bson:"avg_rating,omitempty"`
}

// Create a filter struct to specify the documents to update
type UpdateManyRestaurantFilter struct {
Cuisine string `bson:"cuisine"`
Borough string `bson:"borough"`
}

// Defines a RestaurantUpdate struct to specify the fields to update
type RestaurantUpdateMany struct {
AverageRating float64 `bson:"avg_rating"`
}

func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
Expand All @@ -53,13 +42,10 @@ func main() {
}()

coll := client.Database("sample_restaurants").Collection("restaurants")
filter := UpdateManyRestaurantFilter{
Cuisine: "Pizza",
Borough: "Brooklyn",
}
filter := bson.D{{"cuisine", "Pizza"}, {"borough", "Brooklyn"}}

// Creates instructions to update the values of the "AverageRating" field
update := bson.D{{"$set", RestaurantUpdateMany{AverageRating: 4.5}}}
// Creates instructions to update the values of the "avg_rating" field
update := bson.D{{"$set", bson.D{{"avg_rating", 4.5}}}}

// Updates documents in which the value of the "Cuisine"
// field is "Pizza"
Expand Down
54 changes: 0 additions & 54 deletions source/includes/usage-examples/code-snippets/updateManyBson.go

This file was deleted.

14 changes: 2 additions & 12 deletions source/includes/usage-examples/code-snippets/updateOne.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ type Restaurant struct {
AverageRating float64 `bson:"avg_rating,omitempty"`
}

// Create a filter struct to specify the document to update
type UpdateRestaurantFilter struct {
ID bson.ObjectID `bson:"_id"`
}

// Defines a RestaurantUpdate struct to specify the fields to update
type RestaurantUpdate struct {
AverageRating float64 `bson:"avg_rating"`
}

func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
Expand All @@ -53,10 +43,10 @@ func main() {
coll := client.Database("sample_restaurants").Collection("restaurants")

id, _ := bson.ObjectIDFromHex("5eb3d668b31de5d588f4292b")
filter := UpdateRestaurantFilter{ID: id}
filter := bson.D{{"_id", id}}

// Creates instructions to add the "avg_rating" field to documents
update := bson.D{{"$set", RestaurantUpdate{AverageRating: 4.4}}}
update := bson.D{{"$set", bson.D{{"avg_rating", 4.4}}}}

// Updates the first document that has the specified "_id" value
result, err := coll.UpdateOne(context.TODO(), filter, update)
Expand Down
54 changes: 0 additions & 54 deletions source/includes/usage-examples/code-snippets/updateOneBson.go

This file was deleted.

Loading