Skip to content

Commit 8be9d8e

Browse files
authored
DOCSP-52409 Remove struct filter from update usage ex (#550)
1 parent 0185666 commit 8be9d8e

File tree

5 files changed

+32
-209
lines changed

5 files changed

+32
-209
lines changed

source/crud/update.txt

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -209,94 +209,49 @@ UpdateOne() Example: Full File
209209

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

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

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

218-
.. tab:: Struct
219-
:tabid: structExample
220-
221-
The following code uses a struct to define the filter and update a document in the ``restuarants`` collection:
222-
223-
.. io-code-block::
224-
:copyable: true
225-
226-
.. input:: /includes/usage-examples/code-snippets/updateOne.go
227-
:language: go
228-
:dedent:
229-
230-
.. output::
231-
:language: none
232-
:visible: false
233-
234-
Documents updated: 1
235-
236-
.. tab:: bson.D
237-
:tabid: bsonDExample
238-
239-
The following code uses a ``bson.D`` type to define the filter and update a document in the ``restuarants`` collection:
240-
241-
.. io-code-block::
242-
:copyable: true
218+
.. io-code-block::
219+
:copyable: true
243220

244-
.. input:: /includes/usage-examples/code-snippets/updateOneBson.go
245-
:language: go
246-
:dedent:
221+
.. input:: /includes/usage-examples/code-snippets/updateOne.go
222+
:language: go
223+
:dedent:
247224

248-
.. output::
249-
:language: none
250-
:visible: false
225+
.. output::
226+
:language: none
227+
:visible: false
251228

252-
Documents updated: 1
229+
Documents updated: 1
253230

254231
UpdateMany() Example: Full File
255232
-------------------------------
256233

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

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

263-
.. tabs::
239+
- Finds documents with the ``cuisine`` field value of ``"Pizza"`` and the
240+
``borough`` field value of ``"Brooklyn"``
241+
- Updates the ``avg_rating`` field value of the matched documents
264242

265-
.. tab:: Struct
266-
:tabid: structExample
267-
268-
The following code uses a struct to define the filter and update multiple documents in the ``restuarants`` collection:
269-
270-
.. io-code-block::
271-
:copyable: true
272-
273-
.. input:: /includes/usage-examples/code-snippets/updateMany.go
274-
:language: go
275-
:dedent:
276-
277-
.. output::
278-
:language: none
279-
:visible: false
280-
281-
Documents updated: 296
282-
283-
.. tab:: bson.D
284-
:tabid: bsonDExample
285-
286-
The following code uses a ``bson.D`` type to define the filter and update multiple documents in the ``restuarants`` collection:
287-
288-
.. io-code-block::
289-
:copyable: true
243+
.. io-code-block::
244+
:copyable: true
290245

291-
.. input:: /includes/usage-examples/code-snippets/updateManyBson.go
292-
:language: go
293-
:dedent:
246+
.. input:: /includes/usage-examples/code-snippets/updateMany.go
247+
:language: go
248+
:dedent:
294249

295-
.. output::
296-
:language: none
297-
:visible: false
250+
.. output::
251+
:language: none
252+
:visible: false
298253

299-
Documents updated: 296
254+
Documents updated: 296
300255

301256
Additional Information
302257
----------------------

source/includes/usage-examples/code-snippets/updateMany.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ type Restaurant struct {
2121
AverageRating float64 `bson:"avg_rating,omitempty"`
2222
}
2323

24-
// Create a filter struct to specify the documents to update
25-
type UpdateManyRestaurantFilter struct {
26-
Cuisine string `bson:"cuisine"`
27-
Borough string `bson:"borough"`
28-
}
29-
30-
// Defines a RestaurantUpdate struct to specify the fields to update
31-
type RestaurantUpdateMany struct {
32-
AverageRating float64 `bson:"avg_rating"`
33-
}
34-
3524
func main() {
3625
if err := godotenv.Load(); err != nil {
3726
log.Println("No .env file found")
@@ -53,13 +42,10 @@ func main() {
5342
}()
5443

5544
coll := client.Database("sample_restaurants").Collection("restaurants")
56-
filter := UpdateManyRestaurantFilter{
57-
Cuisine: "Pizza",
58-
Borough: "Brooklyn",
59-
}
45+
filter := bson.D{{"cuisine", "Pizza"}, {"borough", "Brooklyn"}}
6046

61-
// Creates instructions to update the values of the "AverageRating" field
62-
update := bson.D{{"$set", RestaurantUpdateMany{AverageRating: 4.5}}}
47+
// Creates instructions to update the values of the "avg_rating" field
48+
update := bson.D{{"$set", bson.D{{"avg_rating", 4.5}}}}
6349

6450
// Updates documents in which the value of the "Cuisine"
6551
// field is "Pizza"

source/includes/usage-examples/code-snippets/updateManyBson.go

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

source/includes/usage-examples/code-snippets/updateOne.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ type Restaurant struct {
2020
AverageRating float64 `bson:"avg_rating,omitempty"`
2121
}
2222

23-
// Create a filter struct to specify the document to update
24-
type UpdateRestaurantFilter struct {
25-
ID bson.ObjectID `bson:"_id"`
26-
}
27-
28-
// Defines a RestaurantUpdate struct to specify the fields to update
29-
type RestaurantUpdate struct {
30-
AverageRating float64 `bson:"avg_rating"`
31-
}
32-
3323
func main() {
3424
if err := godotenv.Load(); err != nil {
3525
log.Println("No .env file found")
@@ -53,10 +43,10 @@ func main() {
5343
coll := client.Database("sample_restaurants").Collection("restaurants")
5444

5545
id, _ := bson.ObjectIDFromHex("5eb3d668b31de5d588f4292b")
56-
filter := UpdateRestaurantFilter{ID: id}
46+
filter := bson.D{{"_id", id}}
5747

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

6151
// Updates the first document that has the specified "_id" value
6252
result, err := coll.UpdateOne(context.TODO(), filter, update)

source/includes/usage-examples/code-snippets/updateOneBson.go

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

0 commit comments

Comments
 (0)